digitalmars.D.learn - Strange result with nextUp for reals
- Andrej Mitrovic (14/14) Feb 16 2014 -----
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (7/21) Feb 16 2014 That one uses reals.
- "Casper =?UTF-8?B?RsOmcmdlbWFuZCI=?= <shorttail hotmail.com> (3/3) Feb 16 2014 If you swap the line to
-----
import std.math;
import std.stdio;
void main()
{
writefln("nextUp of %a is %a", 1.0, 1.0.nextUp());
real num = 1.0;
writefln("nextUp of %a is %a", num, num.nextUp());
}
-----
This prints:
nextUp of 0x1p+0 is 0x1.0000000000001p+0
nextUp of 0x1p+0 is 0x1.0000000000000002p+0
Any idea why the results are different?
Feb 16 2014
On 02/16/2014 01:42 PM, Andrej Mitrovic wrote:----- import std.math; import std.stdio; void main() { writefln("nextUp of %a is %a", 1.0, 1.0.nextUp());That line uses doubles.real num = 1.0; writefln("nextUp of %a is %a", num, num.nextUp());That one uses reals.} ----- This prints: nextUp of 0x1p+0 is 0x1.0000000000001p+0 nextUp of 0x1p+0 is 0x1.0000000000000002p+0 Any idea why the results are different?There is no difference. They are both 1. ;) The type of floating point literals is double. You are seeing the precision difference between doubles and reals. Ali
Feb 16 2014
If you swap the line to
writefln("nextUp of %a is %a", 1.0L, 1.0L.nextUp());
you get the same result as the second case.
Feb 16 2014








"Casper =?UTF-8?B?RsOmcmdlbWFuZCI=?= <shorttail hotmail.com>