Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

added 1131 characters in body
Source Link
Eric Postpischil
  • 233.9k
  • 15
  • 200
  • 383

In the format commonly used for float, the closest representable value to 1,234,567.89 is 1,234,567.875.

The IEEE-754 binary32 format, also called "single precision," is commonly used for the float type. In this format, numbers are represented as ±f•2e, where f is an integer, 0 ≤ f < 224, and e is an integer, −149 ≤ e ≤ 104. (This is expressible using different scalings for f so that it is not an integer and the range for e is adjusted accordingly so the forms are mathematically equivalent.)

Numbers in this form are encoded using 32 bits:

  • The + or − is encoded in one bit.
  • Of the 24 binary digits of f, 23 are encoded explicitly in 23 bits.
  • The exponent range, −149 ≤ e ≤ 104, spans 254 numbers, so this is encoded in 8 bits. 8 bits can encode 256 numbers, so the other two values are used to represent infinities, NaNs ("Not a Number"), and cases where the leading bit of f is zero.

In this format, the closest representable number to 1,234,567.89 is +9,876,543•2−3 = 1,234,567.875. Since f must be an integer, the next greater representable number is +9876544•2−3 = 1,234,568. (We cannot make the number any finer by using 2−4 instead of 2−3 because then f would have to change to about double of 9,876,543, and that would make it larger than 224.)

So, when atof("1234567.89") returns 1,234,567.875, it is returning the best possible result.

In the format commonly used for float, the closest representable value to 1,234,567.89 is 1,234,567.875.

The IEEE-754 binary32 format, also called "single precision," is commonly used for the float type. In this format, numbers are represented as ±f•2e, where f is an integer, 0 ≤ f < 224, and e is an integer, −149 ≤ e ≤ 104. (This is expressible using different scalings for f so that it is not an integer and the range for e is adjusted accordingly so the forms are mathematically equivalent.)

Numbers in this form are encoded using 32 bits:

  • The + or − is encoded in one bit.
  • Of the 24 binary digits of f, 23 are encoded explicitly in 23 bits.
  • The exponent range, −149 ≤ e ≤ 104, spans 254 numbers, so this is encoded in 8 bits. 8 bits can encode 256 numbers, so the other two values are used to represent infinities, NaNs ("Not a Number"), and cases where the leading bit of f is zero.

In this format, the closest representable number to 1,234,567.89 is +9,876,543•2−3 = 1,234,567.875. Since f must be an integer, the next greater representable number is +9876544•2−3 = 1,234,568.

So, when atof("1234567.89") returns 1,234,567.875, it is returning the best possible result.

In the format commonly used for float, the closest representable value to 1,234,567.89 is 1,234,567.875.

The IEEE-754 binary32 format, also called "single precision," is commonly used for the float type. In this format, numbers are represented as ±f•2e, where f is an integer, 0 ≤ f < 224, and e is an integer, −149 ≤ e ≤ 104. (This is expressible using different scalings for f so that it is not an integer and the range for e is adjusted accordingly so the forms are mathematically equivalent.)

Numbers in this form are encoded using 32 bits:

  • The + or − is encoded in one bit.
  • Of the 24 binary digits of f, 23 are encoded explicitly in 23 bits.
  • The exponent range, −149 ≤ e ≤ 104, spans 254 numbers, so this is encoded in 8 bits. 8 bits can encode 256 numbers, so the other two values are used to represent infinities, NaNs ("Not a Number"), and cases where the leading bit of f is zero.

In this format, the closest representable number to 1,234,567.89 is +9,876,543•2−3 = 1,234,567.875. Since f must be an integer, the next greater representable number is +9876544•2−3 = 1,234,568. (We cannot make the number any finer by using 2−4 instead of 2−3 because then f would have to change to about double of 9,876,543, and that would make it larger than 224.)

So, when atof("1234567.89") returns 1,234,567.875, it is returning the best possible result.

added 1131 characters in body
Source Link
Eric Postpischil
  • 233.9k
  • 15
  • 200
  • 383

In the format commonly used for float, the closest representable value to 12345671,234,567.89 is 12345671,234,567.875.

The IEEE-754 binary32 format, also called "single precision," is commonly used for the float type. In this format, numbers are represented as ±f•2e, where f is an integer, 0 ≤ f < 224, and e is an integer, −149 ≤ e ≤ 104. (This is expressible using different scalings for f so that it is not an integer and the range for e is adjusted accordingly so the forms are mathematically equivalent.)

Numbers in this form are encoded using 32 bits:

  • The + or − is encoded in one bit.
  • Of the 24 binary digits of f, 23 are encoded explicitly in 23 bits.
  • The exponent range, −149 ≤ e ≤ 104, spans 254 numbers, so this is encoded in 8 bits. 8 bits can encode 256 numbers, so the other two values are used to represent infinities, NaNs ("Not a Number"), and cases where the leading bit of f is zero.

In this format, the closest representable number to 1,234,567.89 is +9,876,543•2−3 = 1,234,567.875. Since f must be an integer, the next greater representable number is +9876544•2−3 = 1,234,568.

So, when atof("1234567.89") returns 1,234,567.875, it is returning the best possible result.

In the format commonly used for float, the closest representable value to 1234567.89 is 1234567.875.

In the format commonly used for float, the closest representable value to 1,234,567.89 is 1,234,567.875.

The IEEE-754 binary32 format, also called "single precision," is commonly used for the float type. In this format, numbers are represented as ±f•2e, where f is an integer, 0 ≤ f < 224, and e is an integer, −149 ≤ e ≤ 104. (This is expressible using different scalings for f so that it is not an integer and the range for e is adjusted accordingly so the forms are mathematically equivalent.)

Numbers in this form are encoded using 32 bits:

  • The + or − is encoded in one bit.
  • Of the 24 binary digits of f, 23 are encoded explicitly in 23 bits.
  • The exponent range, −149 ≤ e ≤ 104, spans 254 numbers, so this is encoded in 8 bits. 8 bits can encode 256 numbers, so the other two values are used to represent infinities, NaNs ("Not a Number"), and cases where the leading bit of f is zero.

In this format, the closest representable number to 1,234,567.89 is +9,876,543•2−3 = 1,234,567.875. Since f must be an integer, the next greater representable number is +9876544•2−3 = 1,234,568.

So, when atof("1234567.89") returns 1,234,567.875, it is returning the best possible result.

Source Link
Eric Postpischil
  • 233.9k
  • 15
  • 200
  • 383

In the format commonly used for float, the closest representable value to 1234567.89 is 1234567.875.

lang-c

AltStyle によって変換されたページ (->オリジナル) /