0

how to make the number after point normal ?

// Floating-point arithmetic with float #include <cs50.h> #include <stdio.h> int main(void) { printf("%lu\n", sizeof(float)); // Prompt user for x float x = get_float("x: "); // Prompt user for y float y = get_float("y: "); // Perform division printf("x / y = %.50f\n", x / y); } if I enter 1 and 10 the output shows x / y = 0.10000000149011611938476562500000000000000000000000 why it shows the random number after the 8th bit after the point although the size used to store float is 32 bit..so I suppose the random number should be after the 32th bit after the point ? how can I make the output all the digits after 1 be zeros ?

27th May 2020, 11:21 PM
Sumer Fattoum
Sumer Fattoum - avatar
1 Respuesta
0
Why is it like this: the computer is calculating in binary system. As in decimal we can't 'cleanly' write down 1/3, 1/5 or 1/7, the computer can't cleanly calculate 1/10 (as 10 is not a multiple of 2). (See https://0.30000000000000004.com/) What can you do to prevent that? Round your results. Important note: Don't check equality on floats and doubles as they often should be equal, but differ (in a very small scale). Instead check if they only differ in a very small difference.
28th May 2020, 5:24 PM
Michi
Michi - avatar