Real implicitly converts to float?
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jun 23 12:17:46 PDT 2016
On 06/23/2016 06:57 AM, Steven Schveighoffer wrote:
> 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);
But there is also the representable value range. The difference between
the maximum values are worse in the case of floating point types.
void main() {
pragma(msg, long.max / byte.max);
pragma(msg, real.max / float.max);
}
72624976668147841L
3.49631e+4893L
So it's more nowhere near for floating point types from that point of
view: :)
Ali
More information about the Digitalmars-d-learn
mailing list