Page 1 of 1

Why does Pow() require and return floats?

Posted: Mon Sep 18, 2006 5:14 am
by Amiga5k
Just wondering why both PB and Blitz return floats on something like:
2 ^ 24 ' Blitz

or

Pow(2,24) ; PB

BTW, PB returns the CORRECT integer portion (even though the answer is strangely a float), whereas Blitz does not.

What makes this strange to me is that it is mathematically impossible for an integer taken to the power of an integer to return a fractional result, yet both PB and Blitz do (and probably others as well, haven't checked).

Russell

p.s. Speaking of math, this could be a useful math library to incorporate into PB: http://www.tc.umn.edu/~ringx004/mapm-main.html

What's so great about this library you say? Whereas PB allows up to a dozen and a half or so digits of precision, the MAPM lib allows up to (get this):
BILLIONS of digits! (An extreme example, I know)

Now, you might be thinking "Well, that would probably take a looooooong time to process!". And you'd be right in this most extreme example, but not as long as you might think. Using smart 'FFT' (Fast Fourier Transform) and other tricks, the time can be greatly slashed.

Posted: Mon Sep 18, 2006 5:34 am
by DarkDragon

Code: Select all

Debug Pow(2.5, 2)
Returns float. Everything else is incorrect.

Re: Why does Pow() require and return floats?

Posted: Mon Sep 18, 2006 7:21 am
by Michael Vogel
Amiga5k wrote:Just wondering why both PB and Blitz return floats on something like:
2 ^ 24 ' Blitz

or

Pow(2,24) ; PB
1<<24 :)

Posted: Thu Sep 21, 2006 2:01 am
by Amiga5k
DarkDragon, I'm not sure I follow you. Can you explain what you mean when you say 'everything else is incorrect'?

Russell

Posted: Thu Sep 21, 2006 5:58 pm
by DarkDragon
Amiga5k wrote:DarkDragon, I'm not sure I follow you. Can you explain what you mean when you say 'everything else is incorrect'?

Russell
2,5 ^ 2 can't be 6, it is 6.25. So how should it return an integer if the result is a float?

Posted: Fri Sep 22, 2006 1:11 am
by Amiga5k
Ah, but it returns a float even if the result should be an integer! ;) (But at least PB returns the correct answer, unlike BlitzMax...). The point is that it would be nice if there were two versions of Pow(): One Integer, one Float. Yes, it can be done.

In my example, PB returned 16777216.0 (correct) and BMax returned...Hmm. That's interesting: I just tried it in BMax. If I run 'Print 2 ^ 24' I get 16777216.000000000 (which is correct). But If I run:

Code: Select all

For a = 0 To 31
  Print 2 ^ a
Next
..the answer for 2 ^ 24 becomes 16777215.999999976 for some odd reason (incorrect). I wonder why there are two different outcomes for the same expression? I'll have to run this one by the Blitz forums...

Russell

Posted: Fri Sep 22, 2006 9:51 am
by Flype
Amiga5k wrote:The point is that it would be nice if there were two versions of Pow(): One Integer, one Float. Yes, it can be done.
And what about 'Doubles' and 'Quads' ?

Just like Val(), ValF(), ValD(), ValQ()...

It might be good to have Pow(), PowF(), PowD(), PowQ()... but maybe i'm wrong :roll:

Posted: Fri Sep 22, 2006 1:17 pm
by dracflamloc
you should research the limitations of IEEE floats. Some numbers can't be represented and sometimes your decimal will be off by a minute amount like in the bmax example.