Difference between revisions of "User:Berni44/Floatingpoint"

From D Wiki
Jump to: navigation, search
(Created page with "== An introduction to floating point numbers == ... to be continued")
 
Line 1: Line 1:
 
== An introduction to floating point numbers ==
 
== An introduction to floating point numbers ==
 +
 +
You probably already know, that strange things can happen, when using floating point numbers.
 +
 +
An example:
 +
 +
<syntaxhighlight lang=D>
 +
import std.stdio;
 +
 +
void main()
 +
{
 +
    float a = 1000;
 +
    float b = 1/a;
 +
    float c = 1/b;
 +
    writeln("Is ",a," == ",c,"? ",a==c?"Yes!":"No!");
 +
}
 +
</syntaxhighlight>
 +
 +
Did you guess the answer?
 +
 +
<pre>Is 1000 == 1000? No!</pre>
 +
 +
== Nano floats ==
 +
 +
To understand this strange behavior we have to look at the bit representation of the numbers involved.
  
 
... to be continued
 
... to be continued

Revision as of 18:38, 13 February 2021

An introduction to floating point numbers

You probably already know, that strange things can happen, when using floating point numbers.

An example:

import std.stdio;

void main()
{
    float a = 1000;
    float b = 1/a;
    float c = 1/b;
    writeln("Is ",a," == ",c,"? ",a==c?"Yes!":"No!");
}

Did you guess the answer?

Is 1000 == 1000? No!

Nano floats

To understand this strange behavior we have to look at the bit representation of the numbers involved.

... to be continued