I asked chatGPT why there were rounding error with floating point numbers
This is interesting.
The potential for rounding issues in floating-point arithmetic arises from the way computers represent real numbers. Most computers use a binary representation for floating-point numbers, and not all decimal numbers can be precisely represented in binary. This can lead to rounding errors when converting between decimal and binary representations.
Consider the following example in base-10 (decimal):
1/3 = 0.333333...
Now, in base-2 (binary), representing 1/3 as a finite fraction results in a repeating binary fraction:
1/3 = 0.0101(01)...
In this case, the binary representation is not exact, and it repeats indefinitely. This repeating fraction cannot be represented precisely in a finite number of binary digits, leading to a loss of precision.
Similarly, some decimal numbers cannot be represented exactly in binary, and when these numbers are involved in calculations, rounding errors can accumulate. For example:
0.1 (decimal) = 0.0001100110011001100110011001100110011001100110011...(binary)
When working with floating-point numbers, especially in repetitive or extensive calculations, these rounding errors can accumulate and lead to discrepancies between the expected and actual results.
While floating-point representations are highly useful and versatile for a broad range of applications, developers need to be aware of these limitations and take precautions when precision is critical. Techniques such as rounding, using fixed-point arithmetic, or employing specialized numerical libraries can help mitigate some of the issues associated with floating-point arithmetic.
#include <stdio.h>
int main() {
int num1 = 5;
int num2 = 2;
float sum = (float) num1 / num2;
printf("%f", sum);
return 0;
THE ABOVE WORKS AND THE BELOW WORKS
#include <stdio.h>
int main() {
float num1 = 5;
float num2 = 2;
float sum = num1 / num2;
printf("%f", sum);
return 0;
}
BUT I THINK I WILL GO WITH WHAT W3 SUGGESTS.
float
you mean the raw bits representing its value, it's merely a bit string and your could do anything with it (how about a General Purpose Turing Machine on float point numbers?). However if you mean whatIEEE754
and its extension specifies, obviously not. – Daccadouble
is a 53-bit integer along with an exponent sitting in 64 bits; that's an overhead of about 17%. – Philipinesa = 1e16; a+1 == a
isTrue
. Have you ever came across a more satisfying answer? @Baring – Alto