Real implicitly converts to float?
Steven Schveighoffer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jun 23 06:57:57 PDT 2016
On 6/23/16 5:37 AM, Jonathan M Davis via Digitalmars-d-learn wrote:
> On Thursday, June 23, 2016 04:55:09 Tofu Ninja via Digitalmars-d-learn wrote:
>> Should I make a bug report? I am not sure it's a bug, seems
>> intentional. Maybe a dip for a compiler flag to warn on implicit
>> down conversions, but it would be a pretty small dip.
It's not a bug. Floating point is in general an approximation, so it's
not expected to accurately capture the value. It's not the same as a
narrowing conversion.
For instance:
int x = 1_000_000;
byte b = cast(byte)x;
assert(b == 64);
64 is nowhere near 1 million.
However:
double x = 1_000_000_000_000_000;
float f = x;
assert(f == 999_999_986_991_104);
Now, f and x aren't equal, but they are very close. Much more accurate
than 64 and 1 million. Whenever you work with floating point, the loss
of precision must be expected -- a finite type cannot represent an
infinite precision number.
> You're original code is almost certainly not a bug thanks to VRP
No, VRP only works on the current expression (statement maybe?). The
compiler does not examine previous lines to see what the range of a
particular variable should be.
For example, this is an error:
int x = 10;
byte b = x; // error
This isn't:
int x;
byte b = x = 10;
-Steve
More information about the Digitalmars-d-learn
mailing list