Re: Numerics

josé bollo <[email protected]>
Newsgroups gmane.comp.lang.eiffel.smalleiffel
Message-ID <[email protected]>
Dominique Colnet a écrit :

> josé bollo wrote:
>
>> here is a common expression (where n is 9 and m is INTEGER):
>>
>>   m := 10000*2^n
>>
>> so i apply SE rules: {INTEGER_16}*{INTEGER_8}^{INTEGER_8}
>>
>> and nothing does fit! : 2^9 is an INTEGER_16 and the 5120000 is 
>> obviously not an INTEGER_16.
>>
>> And me the programmer have to be aware of such a thing even when i 
>> know that the result is an INTEGER so INTEGER_32 that fit.
>
> You are right here. The compiler should infer that 10000*2^9 = 5120000
> and thus fit on INTEGER_32.
> Exactly as the compiler is currently able to see that 127+1 = 128
> So you are right and I consider it as a bug in the compiler.
> I have registered your example in our private test suite (no need for 
> you to fill a SmartZilla record).
> Thanks
> And as usual best regards to all,

good

but have tried to write the expression?

let try it again

    n: INTEGER

    check n.in_range(2,17) end
    m := 10_000 * 2^n

here ^^^^^ it is pretty and safe

    check n.fit_integer_8 end
    m := {INTEGER_32 10_000} * 2^(n.to_integer_8).to_integer_32

here ^^^ it is ugly and not safe, n>18 will give a deasaster

    m := {INTEGER_32 10_000} * ({INTEGER_32 2}^n).to_integer_32

here ^^^ it is ugly and not safe but maybe best

    m := 10_000 * ({INTEGER_32 2}^n).to_integer_32

trying the automatic conversion

You see, here except by using a log in preconditions of ^, the check 
n.in_range is the best solution for safety

cheers
josé
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.