I changed the salary float to 56.12 and it printed 56.1199 why??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I changed the salary float to 56.12 and it printed 56.1199 why???

13th Oct 2019, 3:47 AM
Emad
21 Answers
0
Can you send the code also so that it becomes clear how have you changed it.
13th Oct 2019, 3:50 AM
Arsenic
Arsenic - avatar
+ 2
The problem is that when its another number like 13.87 for example it works correctly
13th Oct 2019, 4:20 AM
Emad
+ 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(#).
14th Oct 2019, 5:07 AM
Sir Eric
+ 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; }
13th Oct 2019, 3:53 AM
Emad
+ 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
13th Oct 2019, 11:45 AM
Emad
+ 1
I think by common sense it's approximation
14th Oct 2019, 7:27 PM
Nathan Nworie
Nathan Nworie - avatar
+ 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
15th Oct 2019, 3:15 AM
Bhushan Surjuse
Bhushan Surjuse - avatar
0
Make float into a double, double is more precise.
13th Oct 2019, 4:17 AM
Odyel
Odyel - avatar
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
13th Oct 2019, 8:33 AM
Emad
0
I didnt get what u said im sorry but i just donnow what converting a numeric type to a string representation means
13th Oct 2019, 1:10 PM
Emad
0
Thats great i think im starting to know whats going on Tnx alot for ur time
13th Oct 2019, 4:29 PM
Emad
0
Just some last thing Can u easily tell if a number is going to have a rounding error when printed?
13th Oct 2019, 4:31 PM
Emad
0
Aha🤪
13th Oct 2019, 5:12 PM
Emad
13th Oct 2019, 11:51 PM
Betito Castillo
0
Could u gimme an example code of that setprecision thing?
14th Oct 2019, 5:27 AM
Emad
0
Tnx allllot ur the best i mean it👌👌👍👍
14th Oct 2019, 11:33 AM
Emad
0
You have to tell compiler to print two digit precision like %.2f instead %f
14th Oct 2019, 2:38 PM
Soloarc Dev
Soloarc Dev - avatar
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?
14th Oct 2019, 2:49 PM
storm
storm - avatar
0
Here you use the Printf(%0.2f, c);
15th Oct 2019, 3:51 AM
Rakesh Sisodiya
Rakesh Sisodiya - avatar
0
That happened mostly because you declared the variable for salary as integer
18th Jan 2020, 6:10 AM
Anushka
Anushka - avatar