Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Type inconsistency in Arithmetic Functions

 [1/5] from: pwawood:gm:ail at: 11-Aug-2007 12:04


I feel the following behaviour is inconsistent:
>> type? 2 + 2
== integer!
>> type? 2 - 2
== integer!
>> type? 2 * 2
== integer!
>> type? 2 / 2
== integer!
>> type? 2 ** 2
== decimal! Both Python seems to return an integer:
>>> 2 ** 2
4
>>> 2.0 ** 2
4.0 As does Ruby: irb(main):002:0> 2 ** 2 4 irb(main):004:0> 2.0 ** 2 4.0 Does anybody know if this is by design or is simply a consequence of the current implementation? Regards Peter

 [2/5] from: santilli:gabriele:gm:ail at: 11-Aug-2007 11:24


2007/8/11, Peter Wood <pwawood-gmail.com>:
> Does anybody know if this is by design or is simply a consequence of > the current implementation?
I guess it would just be much slower to do otherwise. Regards, Gabriele.

 [3/5] from: pwawood:gmai:l at: 11-Aug-2007 17:42


> I guess it would just be much slower to do otherwise.
As the average machine has a much faster processor now than when Rebol 2 was conceived and Rebol 3 looks to be even faster than Rebol 2, do you think it would be worth considering returning an integer! from power or ** in Rebol 3 ? Regards Peter

 [4/5] from: carl:cybercraft at: 11-Aug-2007 21:47


On Saturday, 11-August-2007 at 11:24:41 Gabriele Santilli wrote,
>2007/8/11, Peter Wood <pwawood-gmail.com>: > >> Does anybody know if this is by design or is simply a consequence of >> the current implementation? > >I guess it would just be much slower to do otherwise.
Could it be that the results are more likely to be really huge and so can only be handled by decimals?
>> 1000000 * 1000000
** Math Error: Math or number overflow ** Near: 1000000 * 1000000
>> 1000000.1 * 1000000.1
== 1000000200000.01 And that's just with multiplication...

 [5/5] from: santilli:gabriele::gmail at: 12-Aug-2007 10:33


2007/8/11, Peter Wood <pwawood-gmail.com>:
> As the average machine has a much faster processor now than when Rebol > 2 was conceived and Rebol 3 looks to be even faster than Rebol 2, do > you think it would be worth considering returning an integer! from > power or ** in Rebol 3 ?
It's not a matter of how fast the machines are. It's a matter of how inefficient it would be. When it comes to division, you can do integer division, which is usually one op for the processor, then check remainder and if non-zero do the slower floating point division (with all the conversions involved). But when it comes to power, there is no integer power. So you'd have to do the conversions, obtain floating point result, then check if it has no fractional part (which involves rounding, unless you want to return integer sometimes, decimal some other times, in a completely unpredictable way), and if not convert back to int. Since in practice it makes absolutely no difference, this would be a complete waste of time. Regards, Gabriele.