I am trying to filter an array with different elements in using an if statement. So far i have if ( a < 0.001 ) a = 0 which works but i have minus values in the array which i wish to keep. Therefore it would be ideal to have an if statement that said for example if (a = (between 0.000592654 and 0.000592655) a = 0.

I have tryed loads of things but cannot get it to work.

a = (sin(pi*(i*b)/180));

if ( a == 0.000592654 )
a = 0;

cout << a;

The code above is just one way i have tryed which dosnt work because as soon as the value = 0.000592654 is met the rest of the elements after it go to 0. If there is some way of stating if the array is between X and Y output a 0 only

Thanks

## All 13 Replies

use abs .

``if (a[i] > 0.000592654 && a[i] < 0.000592655) a[i] =0;``

This would set any value between the two numbers to 0 (BETWEEN, so 0.000592655 and 0.000592655 are not included)
Is this what you want or did I misunderstand your question?

Hello,

That is more than what i was after! works perfectly now! i tryed using FABS etc
but it didnt quite work. How would i use abs though?

It would be even better if i could adjust the values in the array to display 2 dp.

John

fabs is what you need, it's the float/double version of abs().
What is does is: make a negative number positive, so -1 becomes 1 etc.
You say it didn't work? What didn't work? Did you include the math library? This is required when using (f)abs
example:

``````#include <stdio.h>
#include <math.h>
int main()
{
float absolute = fabs((float)-1.455677);
printf("absolute value is: %2.2f", absolute);
getchar();
return 0;
}``````

This program will turn the negative number into a positive one, and then show it with 2 decimals. (rounded)

here's a link for (f/l)abs.

My lecturer recommended i use fabs

he said sinAngle = fabs(sin( ))<0.0001 ? 0 : sin ( );

but im totally lost.

When i said it dosnt work, i have the math library entered but the output when i type cout << a; still displays the range of values in the array but the problem is one of the values is 0.0000517 or something and the 517 bit plays havoc when printing out a sine wave. It never takes it to be 0.

How can in "play havoc" when 0.0000517 is almost 0.00? They are essentially the same. I suggest it's how you are using 0, not the fact that it's a minuscule number.

which is why im spending hours trying to find the reason why it wont work. I know that sin 180 = 0, ive seen it a million times on a calculator. The fact is my program calculates it for example 0.00000473844 and i need to know how to change this.

Are you in fact asking us to tell you what you are doing wrong? If so, how would we know? We aren't the Psychic Programming Forum.

Have you considered showing us what your code is and explaining
1) what is actually happening and
2) what you want to happen?
Like the post titled Read Me: Read This Before Posting says? (note the title -- it's important) Please do so and we can probably cut this from a three day guessing game to a 2 hour solution session.

Create a small test program which just calculates the sine of 180 degrees, and show us that it doesn't print 0 as a result.

Then post that WHOLE program (not random snippets where you think the problem is), then perhaps we can explain what is really going on and what you can really (perhaps) do about it to solve the problem.

For one thing, you've never stated what value of pi you're using.

I have asked this on a previous post except the replies were to complex for me (a beginner) to understand. This post I have questioned a different approach i have thought of and tried to link it back to the original problem.

I have explained a calculation is used and the results from this are stored into an array. I have shown this calculation and the array it’s in and stated my problem under a different method approach and then half way through linked back to my original problem.

anyway:

i and b are part of a user number entered

for (int i=0; i<=c; i++ )

a = (sin(pi*(i*b)/180));

I need the values from the calculation in the array to be in the form 0.00 2d.p not 0.000000. I have been told to use fabs etc but I am completely lost in how to use to use it.

Thanks for that m8!, most hopeful.

I have already experiemented using setprecision with a cout statement which works a treat shame i cant store those values like that.

Well you can.
There is such a thing as "fixed point" arithmetic, but there is no native support in the language to implement it.

You would have to implement your own class, overload things like +, *, = and then write your own trig functions to make use of the mathematical primitives.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.