Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [c] scanf and floats (https://thetfp.com/tfp/tilted-technology/108677-c-scanf-floats.html)

Digilogic 09-18-2006 10:55 PM

[c] scanf and floats
 
I am using scanf to enter a float as follows:

float paymentAmount = 0.0;

(void) scanf ("%f", &paymentAmount);

When I enter a value such as 13.46 the standard printf shows something like 13.46000000

but if I set the printf to show more places it turns out that the stored number is really 13.4600000012343542....

this is throwing off my calculations..

Any suggestions as to why those random numbers are at the end of my float?

n0nsensical 09-19-2006 04:17 AM

Yes, that's the actual value of the stored number. As a consequence of limited accuracy, it's impossible to precisely represent all real numbers, and as a consequence of the binary floating point format, this includes many numbers that "look nice" in decimal such as 13.46. The computer does the best it can in giving you the closest value that is possible. If you need more accuracy, you can use doubles, but integers are the only built-in types that will give you exact accuracy (at the cost of range). For example, you can represent dollar values in terms of integer cents instead of decimal dollars.

xepherys 09-21-2006 01:10 PM

You could also force it to always only look at 2 decimal places. If you are doing a calculation to say add two numbers (not likely where your math goes wonky, but it works just the same), instead of doing:

$sum = $a + $b

you can do something like:

$sum = round($a, 2) + round($b, 2)

The above example is in PHP, but I'm sure C has a similar function. This will round $a to say, 13.46 instead of 13.46000000 or 13.46000001234542 and will perform the math on that value.

Dilbert1234567 09-21-2006 02:29 PM

how about using an integer, and making it represent cents instead of dollars?

Pragma 09-22-2006 07:26 AM

You may be able to do scanf( "%d.%d", &intOne, &intTwo); and that should work, assuming you instruct the user to enter it in the format of "XX.XX", where the Xes are obviously integers. Not sure 100% sure how to get around the float issue, my C is a little rusty at the moment.

a-j 09-22-2006 09:30 PM

Use doubles
 
If you just want more accuracy, use doubles. I think printf/scanf need %fl in that case.

captobvious 09-22-2006 11:29 PM

Quote:

Originally Posted by a-j
If you just want more accuracy, use doubles. I think printf/scanf need %fl in that case.

It might have just been a typo, but it's %lf to read/print a double with printf/scanf.


All times are GMT -8. The time now is 01:50 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76