+ 1
I changed the salary float to 56.12 and it printed 56.1199 why???
21 ответ
0
Can you send the code also so that it becomes clear how have you changed it.
+ 2
The problem is that when its another number like 13.87 for example it works correctly
+ 2
It is impossible for memory to store decimal floats exactly because computers have to use binary in 32 bytes to represent untold digits. Using double gives it more memory to be more precise, but it will still never be exact. After 7 or so, the numbers will be meaningless. If you need more precise significant digits, use setprecision(#).
+ 1
#include <stdio.h>
int main() {
int a, b;
float salary = 56.12;
char letter = 'Z';
a = 8;
b = 34;
int c = a+b;
printf("%d \n", c);
printf("%f \n", salary);
printf("%c \n", letter);
return 0;
}
+ 1
First of all im so sorry that u got offended i did not mean anything bad by that
And about the question i totally understood what u just explained but why does it print the exact amount of 56.23 then? Try it and u will see. The fraction part isnt a power of 2
And in the last im sorry again about earlier but if u thaught or did even a bit diffrently because of my country i cant forgive that with respect
+ 1
I think by common sense it's approximation
+ 1
Both Double and float data type are used torepresent floating point numbers , adouble data type is more precise than float. Adouble variable can provide precision up to 15to 16 decimal points as compared to floatprecision of 6 to 7 decimal digits
0
Make float into a double, double is more precise.
0
I think u didnt get the question dude the problem is that it sometimes prints the exact number and sometimes doesnt look at the codes above and check the results
0
I didnt get what u said im sorry but i just donnow what converting a numeric type to a string representation means
0
Thats great i think im starting to know whats going on
Tnx alot for ur time
0
Just some last thing
Can u easily tell if a number is going to have a rounding error when printed?
0
Aha🤪
0
Could u gimme an example code of that setprecision thing?
0
Tnx allllot ur the best i mean it👌👌👍👍
0
You have to tell compiler to print two digit precision like %.2f instead %f
0
Thanks for your answers Miika , they are very instructive.
I would like to go a step further and ask wether this "rounding problem" is language and/or compiler specific. i.e. does it happen in every C compiler? and does it happen in all of the languages? or only insome of the languages?
0
Here you use the
Printf(%0.2f, c);
0
That happened mostly because you declared the variable for salary as integer