Re: My scripting language - any suggestions?



On Sun, Aug 31, 2008 at 9:58 PM, Dmitry A. Kazakov
<mailbox@xxxxxxxxxxxxxxxxx> wrote:
If you use hardware-supported 32-bit representation of floats then
there will be a problem with precision. Some Int values cannot not be
precisely represented as floats.

That is up to inherited operations. Basically, if Integer inherits
anything from Float it also does the property of Float being an
interval of [real] numbers, with the consequences of. If Integer can
do this operation better, then it should override. The third
alternative is adding ideal values to the class in the form of NaN or
else an exception propagation.


Sorry, I still don't get how the inheritance thing alone could
automagically resolve all the subtle numeric issues.

Suppose we have a method that simply returns the sum of two args:

m(a, b) { return a + b; }

I could understand what happens If both args have the same type.
For instance if ints and floats both use 32-bit representation then

m(1, 2000000000)

will quite obviously result in 2000000001

while

m(1.0, 2000000000,0)

will result in 2000000000.0 - 32-bit floats do not have enough
precision to keep the '1' in the end.

Now what will happen if one argument is int and another
is float ?

m(1, 2000000000.0)

will it be 2000000001, or 2000000000,0, or a runtime error?

In any case the user might prefer one way or another. In
the last example if the user wants to preserve the precision
then this could be done by converting float to int. On the
other hand for m(1, 1.0e15) conversion to int will not work
so it should not be done.

So do you reserve the possibility for a user to do the explicit
conversion? Or never is never and explicit conversion is always
a design problem?

Regards,
Aleksey
.



Relevant Pages

  • Re: pointer and array
    ... impossible to compare the values, ... int 3 and the float 3.14, that are equal when we convert them both ... the "double" 33554433.0 becomes 33554432.0 after conversion to ...
    (comp.lang.c)
  • Re: double to int conversion yields strange results
    ... giving me two different values for the int. ... the question raised is not about the precision of the ... is done in extended precision. ... It is the conversion to 'double' before the conversion to 'int' that ...
    (comp.lang.c)
  • Re: Floating point rounding
    ... Some compilers may not do this conversion compile-time. ... casting float pointers to int pointers is a violation of the strict-aliasing rule. ...
    (comp.graphics.algorithms)
  • Re: Type conversion question
    ... I know that C does some implicit type conversion like integer ... promotion and float to double. ... int main{ ... char cc = ii; ...
    (comp.lang.c)
  • Re: Integer to Floating-Point conversions
    ... Kai Koehne wrote: ... I need an int to real32 conversion with IEEE-754 round-towards-zero mode ... There's no way to squeeze 32 bits of precision into ... less than 32 bits of precision, expecially when a 32 bit float needs a ...
    (comp.lang.java.programmer)