Skip to content

Instantly share code, notes, and snippets.

@noname007
Created October 18, 2018 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noname007/bf25c7f053d164a833ddfd881741d4e0 to your computer and use it in GitHub Desktop.
Save noname007/bf25c7f053d164a833ddfd881741d4e0 to your computer and use it in GitHub Desktop.
#include
int main() {
double a = 0.58, *aptr = &a;
float af = 0.58, *afptr = ⁡
printf(“%lu,%lx\n”,sizeof(a), *(long int *)aptr);
printf(“%lu,%x\n”,sizeof(af), *(int *)afptr);
// printf(“%lu,%d”,sizeof(a),(int) (a * 100) );
// int b = 21;
// printf(“%lu, %p\n”, sizeof(b), b);
// printf(“%lu, %d\n”, sizeof(b), (int)(float) b);
return 0;
}
@noname007
Copy link
Author

noname007 commented Oct 18, 2018

> (= 1.000000001 1.000000002)
#f
> (= 1.0000000000001 1.0000000000002)
#f
> (= 1.000000000000001 1.000000000000002)
#f
> (= 1.0000000000000001 1.0000000000000002)
#f
> (= 1.00000000000000001 1.00000000000000002)
#t
> (= 10000000000000000.1 10000000000000000.2)
#t

@noname007
Copy link
Author

noname007 commented Oct 18, 2018

<?php
$b = 1.0001;
$c = 1.0002;
var_dump($b == $c); // Y:F B:T X:T -------> false


$b = 1.000000001;
$c = 1.000000002;
var_dump($b == $c);  //Y:F B:F X:F -------> false

$b = 1.00000000000000001;
$c = 1.00000000000000002;
var_dump($b == $c); //Y:T B:T X:T -------> true


$b = 1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001;
$c = 1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002;
var_dump($b == $c); //Y:T B:T X:~ -------> true

@noname007
Copy link
Author

noname007 commented Oct 19, 2018

from abaove zhihu link

image

image

@noname007
Copy link
Author

noname007 commented Oct 19, 2018

问题:

  • 浮点数能表示的最大数,(非正无穷) 是个整数,还是个浮点数
  • printf 的输出
    float c = (1 << 24);  // 16777216.000000
    float b = c - 1, d = c + 1, e = d + 1, f = e + 1; // 16777215.000000 16777216.000000 16777216.000000 16777216.000000 16777216.000000
    printf("%f %f %f %f %f\n",b, c, d, e, f);

    //float b = c - 1, d = c + 1, e = c + 2, f = c + 3;//16777215.000000 16777216.000000 16777216.000000 16777218.000000 16777220.000000
    //printf("%f %f %f %f %f\n",b, c, d, e, f);

@noname007
Copy link
Author

@noname007
Copy link
Author

@noname007
Copy link
Author

@noname007
Copy link
Author

100.000100000000000332 == 100.000100000000000333 // true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment