Promotion rules ... why no float?

Nick Treleaven via Digitalmars-d digitalmars-d at puremagic.com
Wed Sep 7 09:04:05 PDT 2016


On Wednesday, 7 September 2016 at 15:15:03 UTC, John Colvin wrote:
> python3 uses / for floating point division and // for integer. 
> I really like the distinction, although I would prefer if / was 
> disallowed on integer operands entirely, i.e. 3/2.0 is ok but 
> 3/2 is not, that would be an error and you'd have to do 3 // 2

Personally I agree. A more nuanced solution is filed here:
https://issues.dlang.org/show_bug.cgi?id=12452

At the end of the description, Don is quoted:

> It is indeed a common floating-point bug.
>
> I came up with a solution for this a couple of years ago, never
> got around to doing a pull request, but it's on the newsgroup
> somewhere. It's a little extension to the range propagation
> implementation. You add a boolean flag to the range, which
> indicates 'a fractional part has been discarded'. This flag gets
> set whenever you perform an integer division (or integer
> exponentiation with a negative power), and is cleared whenever
> there is a cast or a bitwise operation.
>
> Then, disallow implicit casting from integer to floating point
> whenever the fractional bit is set. Catches all these kinds of
> bugs, doesn't require any changes to the language.


More information about the Digitalmars-d mailing list