bmx-ng / bcc

A next-generation bcc parser for BlitzMax
zlib License
33 stars 12 forks source link

x:double mod 0 ... is "nan" #531

Closed GWRon closed 4 years ago

GWRon commented 4 years ago

I already mentioned it in the issue https://github.com/bmx-ng/bcc/issues/527 but maybe it got a bit lost there ...

The illegal instruction bug is fixed now, but the following code does not result in a "division by zero error":

SuperStrict
Framework Brl.Blitz
Import Brl.StandardIO

If 100:Double Mod 0 = 0 Then End
Print (100:Double Mod 0)

It just outputs:

-nan

I am not absolutely sure (dunno what "fmod" outputs - and gives for errors) but I would have thought it leads to a "division by zero" too.

I might be no "big issue" - but assume mod 0 is mod X... and someone wants to print everytime the remainder is not 0 ...

local X:Int = 0 'coming accidentally from somewhere in a faulty algorithm
If (100:Double Mod X) Then Print "is true"
If 100:Double Mod X <> 0 Then Print "is true"

In both cases this is printed ... - yes, the code is "by intention" exotic and most probably nowhere used. If the first number was a "integer", BCC would catch it with a "division by zero" - but for double it might lead to "unexpected" behaviour - maybe leading to a bug (the "mod 0") not being spotted by the developer.

But as said ... dunno how "fmod" (or other languages than C) handle it.

HurryStarfish commented 4 years ago

Floating point numbers never cause division-by-zero errors; that's by design and not specific to BlitzMax.

GWRon commented 4 years ago

https://en.cppreference.com/w/cpp/numeric/math/fmod

Error handling Errors are reported as specified in math_errhandling.

Domain error may occur if y is zero.

If the implementation supports IEEE floating-point arithmetic (IEC 60559),

If x is ±0 and y is not zero, ±0 is returned If x is ±∞ and y is not NaN, NaN is returned and FE_INVALID is raised If y is ±0 and x is not NaN, NaN is returned and FE_INVALID is raised If y is ±∞ and x is finite, x is returned. If either argument is NaN, NaN is returned

what is this "domain error" ?

https://en.cppreference.com/w/cpp/error/domain_error

so ... isn't it throwing an exception then?

HurryStarfish commented 4 years ago

On the same page you linked (https://en.cppreference.com/w/cpp/numeric/math/fmod):

If a domain error occurs, an implementation-defined value is returned (NaN where supported)


so ... isn't it throwing an exception then?

No. Integer division by zero doesn't throw an exception either, at least in the BlitzMax sense of the term. It's an error on the same level as a stack overflow or a segfault and terminates your program. Technically there are ways to catch even these, but that's not really a rabbit hole we wanna go down, I think...

GWRon commented 4 years ago

OK