0

I need to specify that I need two numbers be the same upon 3rd decimal place: 1.2345 and 1.2348 is correct. But 1.2345 and 1.2388 is not correct. And I need let user specify how many places should program check.

I was thinking about something like that:

do {
   x = f(i++);// will count some number with i iterations
   x_next = f(i++);// will count some number with i+1 iterations
} while (fabs(x - x_next) > accuracy);// there should be some difference, cause more iterations = accurate number, but different numbers = different iterations needed

But I don't know how should I convert number 3 to 0.001.

Can you suggest me something please?

sczdavos
  • 2,035
  • 11
  • 37
  • 71
  • 2
    **be careful** You will want to read [**The Floating-Point Guide - What Every Programmer Should Know ...**](http://floating-point-gui.de/). – David C. Rankin Nov 18 '14 at 18:39
  • What about 1.2344 and 1.2346, which will be rounded down and up, respectively? What about 1.2348 and 1.2352? – M Oehm Nov 18 '14 at 18:57

5 Answers5

1

Divide 1.0 by 10 3 times to get 0.001.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
1

To convert 3 to .001 use the pow() function. .001 = pow(10, -3) (It returns base to the power of exponent, in this case 10^-3.) You would need to include the math.h library to use pow().

A word of caution. abs(x-y)<.001 does not guarantee that they agree on 3 decimal places. For example, 1.00000 and .99999 don't agree on any decimal places but abs(1.00000-.99999)=.00001 < .001.

TravisJ
  • 1,592
  • 1
  • 21
  • 37
  • Well thats correct but there is problem just with repeating 9 right? So if am sure that this cannot occur in my program I should be ok? Is there some better way how to do that? – sczdavos Nov 18 '14 at 18:49
  • I hesitate to say that it is only repeating 9's that have this effect, but I cannot come up with other examples. In your case, you might have something like 1.234000 and 1.233999, they agree on the first 2 decimal places, but not the 3rd (not counting the 1. out front). But again, this is repeating 9's. – TravisJ Nov 18 '14 at 18:57
1

If you need to check if two numbers are the same upon 3rd decimal place, you can simply multiply both values with 1000 and compare them as integers.

You get the picture, you have to mutiply with 10^decimal_place.

EDIT: If rounding is required, then simply add 5/10^(decimal_place+1) before multiplying.

guest
  • 51
  • 3
1

Well, there is two ways to approach this:

  • If you want to see if the difference of two numbers is less than 10 to the power of minus your number (in your example 0.001), you can use the solutions provided. However, it says 1.3458 is equal to 1.3462, which doesn't seems what you wanted.

  • You can convert the numbers to integers before. In your example (3 decimal places), you can multiply your number by 1000 (10 to the power of 3), and get it's integer part (with an (int) cast), as in:

    int multiplier = pow(10,decimalPlaces);
    int number1 = (int) numberOriginal1*multiplier;
    int number2 = (int) numberOriginal2*multiplier;
    if(number1 == number2)
        printf("Success\n");
    else printf("Fail\n");
    

Hope that helps.

Fernando Aires
  • 532
  • 2
  • 7
-1

If you just want to give it as an output simply use printf("%.3f",number)

or if you want to it other way, just go through this question, Rounding Number to 2 Decimal Places in C

Community
  • 1
  • 1
nutrino
  • 11
  • 5
  • Nope, I need to check the accuracy. – sczdavos Nov 18 '14 at 18:50
  • Well, you could also do the output routine do the rounding for you and compare two strings that you have created with `snprintf()`. The standard output routines have mose cases covered that hand-written floating-point code usually doesn't get right. – M Oehm Nov 18 '14 at 18:54