Why does Pow() require and return floats?

Everything else that doesn't fall into one of the other PB categories.
Amiga5k
Enthusiast
Enthusiast
Posts: 329
Joined: Fri Apr 25, 2003 8:57 pm

Why does Pow() require and return floats?

Post 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.
*** Diapers and politicians need to be changed...for the same reason! ***
*** Make every vote equal: Abolish the Electoral College ***
*** www.au.org ***
DarkDragon
Addict
Addict
Posts: 2347
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

Code: Select all

Debug Pow(2.5, 2)
Returns float. Everything else is incorrect.
bye,
Daniel
User avatar
Michael Vogel
Addict
Addict
Posts: 2820
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

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

Post 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 :)
Amiga5k
Enthusiast
Enthusiast
Posts: 329
Joined: Fri Apr 25, 2003 8:57 pm

Post by Amiga5k »

DarkDragon, I'm not sure I follow you. Can you explain what you mean when you say 'everything else is incorrect'?

Russell
*** Diapers and politicians need to be changed...for the same reason! ***
*** Make every vote equal: Abolish the Electoral College ***
*** www.au.org ***
DarkDragon
Addict
Addict
Posts: 2347
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post 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?
bye,
Daniel
Amiga5k
Enthusiast
Enthusiast
Posts: 329
Joined: Fri Apr 25, 2003 8:57 pm

Post 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
*** Diapers and politicians need to be changed...for the same reason! ***
*** Make every vote equal: Abolish the Electoral College ***
*** www.au.org ***
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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:
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post 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.
Post Reply