Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / Exact Math Functions (DBP)

Author
Message
lower logic
18
Years of Service
User Offline
Joined: 15th Jun 2006
Location:
Posted: 30th Aug 2006 04:52 Edited at: 30th Aug 2006 04:53
This is a set of functions that can be used in the rare cases where you need to calculate mathematical operations as exact as possible. Here is a list of the functions you can use:

string = Add(string, string)
string = Subtract(string, string)
string = Multiply(string, string)
string = Divide(string, string)
string = Square_root(string)

Since division can result in a number with an infinite decimal, you may specify how accurate you want the calculation to be via the math.sigfig variable which will limit division calculations to a certain number of significant figures.

Of course, the numbers are passed to and returned from the functions in strings in order to preserve accuracy. The numbers can be virtually any size, not limited to the size of floats or dwords.

The square root algorithm is very slow as it must do repeated division, multiplication, and addition.

The sigfig parameter only applies to division and square roots. Addition, Subtraction, and Multiplication are not affected by the sigfig value in any way.

There are a few known bugs:
Sometimes the division used to put the decimal place in the wrong spot. I really just hacked the algorithm together, so if you find 2 numbers that don't divide right, let me know and I'll try to figure out how to fix it. I think it is fixed and division works fine for most numbers but there's a chance it pops up again.

The square root function doesn't always return the right answer. For example, it returns 2.9999999999999999 (up to math.sigfig decimal places) for the square root of 9.

Here are the functions:

Login to post a reply

Server time is: 2024-11-23 03:55:54
Your offset time is: 2024-11-23 03:55:54