Double Digits in C ++

The IEE754 floating point (64 bits) should correctly represent 15 significant digits, although the internal representation has 17 ditigs. Is there a way to force the 16th and 17th digits to zero?

Ref: http://msdn.microsoft.com/en-us/library/system.double ( VS.80) .aspx :,.

Remember that a floating point number can only approximate a decimal number and that the precision of a floating point number determines how accurately this number approaches a decimal number. By default, a double value contains 15 decimal digits of precision, although no more than 17 digits are supported internally. The precision of a floating point number has several consequences:,.

Example: d1 = 97842111437.390091
 d2 = 97842111437.390076
d1 and d2 differ in 16th and 17th decimal places, which should not be significant. Looking for ways to get them to zero. those. d1 = 97842111437.390000 d2 = 97842111437.390000

+3
source share
5 answers

No. Counter-example: two closest floating point numbers to rational

1.11111111111118

(which contains 15 decimal digits)

1.1111111111111799942818834097124636173248291015625
1.1111111111111802163264883347437717020511627197265625

In other words, there is no floating point number that starts with 1.1111111111111800.

+11
source

. , . math 10. (0,1 - !) . , , , .

+3

, , ( GCC):

GCC

-ffloat-store , .

, 68000, ( 68881) . x86. , IEEE. -ffloat-store , , .

+3

, . , , , . , ; , , .

#include <stdio.h>

union double_int {
  double             fp;
  unsigned long long integer;
};

int main(int argc, const char *argv[])
{
  double            my_double = 1325.34634;
  union double_int  *my_union = (union double_int *)&my_double;

  /* print original numbers */
  printf("Float   %f\n", my_double);
  printf("Integer %llx\n", my_union->integer);

  /* whack the sign bit to 1 */
  my_union->integer |= 1ULL << 63;

  /* print modified numbers */
  printf("Negative float   %f\n", my_double);
  printf("Negative integer %llx\n", my_union->integer);

  return 0;
}
0

, - ( " x" ) . stringstream sprintf.

==; . , (, epsilon() ).

Playing with number bits directly is not a great idea.

0
source

Source: https://habr.com/ru/post/1719307/


All Articles