-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|522|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Snippet Home -> Variables


 
Blitz3Dman
Created : 14 April 2007
Edited : 14 April 2007
Language : Blitz

Root() function

Much more powerful than Blitz Sqr() function

thanks to this website I was finally able to write this function I've been wondering how to write for a long long time.



It's just like Sqr() except it can find any root. (3rd root 4th root 5th root skip a few roots 99th root 100th root etc.)

 

Comments


Saturday, 14 April 2007, 15:21
JL235
Or you could just use the power sign, which is far faster when on it's own (I get around a factor of 4).


Saturday, 14 April 2007, 16:07
Blitz3Dman
Oh.. oh well, at least I had the pride of making one anyway
Friday, 20 April 2007, 08:33
Paul
Actually I'm finding the first code slightly faster. about 5%
Don't know if it's the division slowing it down.
Nice work both of you
Friday, 20 April 2007, 11:15
JL235
I get quite a varying range of results. Wrapped in a function, mine might be slightly slower, maybe around 5%. But I'd say that 5% is too close or small to definately say it's slower. It may vary slightly from machine to machine.

What is odd, is that outside a function, with both lines on their own, I find mine is almost twice as fast.

What should also be noted is that I'd never bother wrapping my snippit in a function, but I would with Blitz3DMan's formula. However as wrapping it in a function makes the most significant difference in performance, I'd say the performance difference must be so neglegable that it wouldn't matter in the vast majority of cases.
Saturday, 28 April 2007, 14:07
Blitz3Dman
Weee!

I'm reading my C++ book again and I got to where it tells you how to code functions so I remade my root function in C++ for anybody who cares

unfortunately I don't know what the ^ would become in C++.

(requires the include math.h)


Saturday, 28 April 2007, 23:30
hyruleknight
well the ^ would be the same. i have only seen the mod command change in languages and that was just to %. same say of doing it just change mod with %
Sunday, 29 April 2007, 05:49
Blitz3Dman
Using both ^ and % the compiler tells me
invalid operands of types `float' and `double' to binary `operator%'

Saturday, 23 January 2010, 10:54
Zakk
I've seen ** used instead of ^, it was very odd to me.
Saturday, 23 January 2010, 11:26
Phoenix
C++ does not have a power operator. ^ is the xor operator; use the pow function from the standard library for powers.

Any inherent speed differences between the two methods are negligible. Performance bottlenecks arise from other causes.
Sunday, 24 January 2010, 19:43
mindstorm8191
JL, your code may or may not be faster, but from what I understand, computers convert those expressions to Blitz3DMan's example anyways. Your code may look more decent, but if you have something like 2^1.5, that's not something easily evaluated. exp(log(2)*1.5), on the other hand, can be done on pen&paper.
Sunday, 24 January 2010, 20:47
JL235
mindstorm8191 JL, your code may or may not be faster, but from what I understand, computers convert those expressions to Blitz3DMan's example anyways.
I disagree. If I was getting a difference in speed then I'd predict that my code and his code were compiling down to different native code.

mindstorm8191 Your code may look more decent, but if you have something like 2^1.5, that's not something easily evaluated. exp(log(2)*1.5), on the other hand, can be done on pen&paper.

Maybe, but if I was going to calculate the values by hand I'd use a calculator (or more likely an interactive programming language) instead of pen and paper. In which case mine is shorter to type.

But I now believe that the most important aspect is clarity. My code is easier to understand in terms of what is happening. This is more important then you think.