Cube Root function?

BlitzMax Forums/BlitzMax Beginners Area/Cube Root function?

EOF(Posted 2010) [#1]
Has anyone got a cube root function?


GfK(Posted 2010) [#2]
I think this is right:
Function cubeRoot:Double(value:Double)
	Return Exp(Log(value) / 3.0)
End Function



Czar Flavius(Posted 2010) [#3]
Root n of x, is x to the power of 1/n.

So square root is x to the power of a half.
Cube root is x to the power of a third.

Return x ^ (1.0/3.0)

The fraction has a decimal part to make them floats, otherwise 1/3 = 0 with integer division. (For beginners' reference)

Last edited 2010


Jesse(Posted 2010) [#4]
for what it's worth and the way floats work with the computer I think GfK's is a little bit more accurate but you might want to do some test first.


EOF(Posted 2010) [#5]
Thanks. That's what I was after. A nice and simple solution


Czar Flavius(Posted 2010) [#6]
Strict

Function gfk:Double(value:Double)
	Return Exp(Log(value) / 3.0)
End Function

Function czar:Double(value:Double)
	Const factor:Double = Double(1.0)/Double(3.0)
	Return value ^ factor
End Function

Print gfk(534.232)
Print czar(534.232)


8.1141549711264869

8.1141549711264851


Windows Calculator gives 8.1141549983193462048021708191752

8.11415499
8.11415497

Both results are equal up to this point, and both are wrong on this digit. So I guess which is more accurate is a moot point, as they are both inaccurate. Assuming Windows calculator can be trusted.

On speed tests, my function is a bit faster.

Last edited 2010


Jesse(Posted 2010) [#7]
I did the test with whole numbers on my Mac with a few number and it was of by .000000000001 with jfk sample; with Czar its of by 6 digits. I know it's not that important as most computations wont require that much precision but it's something I felt the need to mention.
Apparently windows and mac don't both work the same way. mine is an intel Mac core 2 duo. for my purpose I'm going to have to remember to use GfK s example for my mac.
one more thing, I used floats. I do realize that the built in functions use double but I was just doing some simple test since I didn't have much time and I was on my way out.

maybe somebody else with a mac want to prove me wrong.

Last edited 2010

Last edited 2010


Jesse(Posted 2010) [#8]
I hate this new way of editing posts.

Last edited 2010


Floyd(Posted 2010) [#9]
This brings up a frequently overlooked point. The default floating point type for numeric literals is single precision. Presumably this is because BlitzMax was intended primarily for games and 3D graphics hardware normally uses single precision. I don't think "game programming language" is a fair description of BlitzMax now, but it was one of the design criteria.

In any case it does lead to some annoying quirks when you want true double precision. Note that Double(534.232) and 534.232:Double are not the same. The first one tells the compiler to start with the numeric literal 534.232, convert it to a number ( in default single precision ) and finally convert that to double. The second one starts with the numeric literal and builds a double precision value. This is the one you want.

Function gfk:Double(value:Double)
	Return Exp(Log(value) / 3.0)
End Function

Function czar:Double(value:Double)
	Const factor:Double = Double(1.0)/Double(3.0)
	Return value ^ factor
End Function


Print
Print "534.232"
Print Double(534.232)
Print 534.232:Double
Print
Print gfk(534.232)
Print czar(534.232)
Print
Print gfk( 534.232:Double )
Print czar( 534.232:Double )
Print "8.1141549983193462048021708191752"



peltazoid(Posted 2010) [#10]
I complied a version of the program under c++ using the standard libraries with gnu g++ v4.4.3

it returns with 32 character precision

8.1141549983193446138329818495549

calc in ubuntu gives the following (thats with it all calculations using 32 significant places)

8.1141549983193462048021708191752