Move decimal point to the right in c

I am new to C, and when I run the code below, the value that is issued is 12098 instead of 12099.

I know that working with decimal places always involves some degree of inaccuracy, but is there a way to accurately translate the decimal point to the right two places every time?

#include <stdio.h> int main(void) { int i; float f = 120.99; i = f * 100; printf("%d", i); } 
+5
source share
5 answers

Use the round function

 float f = 120.99; int i = round( f * 100.0 ); 

Remember, however, that a float usually has only 6 or 7 digits of precision, so there is a maximum value at which this will work. The smallest float that will not convert correctly is number 131072.01 . If you multiply by 100 and the round, the result will be 13107202 .

You can expand the range of your numbers using double values, but even double has a limited range. (A double has 16 or 17 digits of precision.) For example, the following code will print 10000000000000098

 double d = 100000000000000.99; uint64_t j = round( d * 100.0 ); printf( "%llu\n", j ); 

This is just an example, finding the smallest number is that double precision is left as an exercise for the reader.

+5
source

Use fixed point arithmetic for integers:

 #include <stdio.h> #define abs(x) ((x)<0 ? -(x) : (x)) int main(void) { int d = 12099; int i = d * 100; printf("%d.%02d\n", d/100, abs(d)%100); printf("%d.%02d\n", i/100, abs(i)%100); } 
+4
source

Your problem is that floats are represented internally using IEEE-754 . That is, in base 2, and not in base 10. 0.25 will have an accurate representation, but 0.1 does not and does not have 120.99 .

What really happens is that because of the floating point inactivity, the float ieee-754, closest to the decimal value of 120.99 , multiplied by 100 , is slightly lower than 12099 , so it is truncated to 12098 . The compiler should have warned you that you have a truncation from float to (my did).

The only reliable way to get what you expect is to add 0.5 to the float before truncating to int:

 i = (f * 100) + 0.5 

But beware floating point is inherently inaccurate when processing decimal values.

Edit:

Of course, for negative numbers it should be i = (f * 100) - 0.5 ...

+2
source

If you want to continue working with the number as a floating point number, the answer will more or less not . There are various possibilities for small numbers, but as the number of your numbers increases, problems arise.

If you only want to print the number, then my recommendation would be to convert the number to a string, and then move the decimal point there. This can be a bit complicated depending on how you represent the number in the string (exponential and what not).

If you want this to work and you don't mind using a floating point, I would recommend exploring any number of fixed decimal libraries.

+1
source

you can use

 float f = 120.99f 

or

 double f = 120.99 

by default, c saves floating point values ​​as double, so if you store them in a float variable, implicit casting happens, and this is bad ...
I think it works.

0
source

Source: https://habr.com/ru/post/1212636/


All Articles