PDA

View Full Version : Unlimited length floating point calculator

LuckY
11-29-2004, 02:22 PM
So I've been working on this HugeFloat class for a little while and just wrote a little sloppy calculator to try using it (and found a couple of minor problems). I was just hoping some of you might not mind doing the same. I'd appreciate any comments or suggestions or bug observations.

I was also thinking of making this little app more like Windows calc.exe (allowing keypresses to do things instead of only mouse clicks), but it wasn't cooperating with my efforts and I didn't want to waste time trying to figure it out (specifically trying to catch keypresses and simulate the corresponding button being depressed and released). If you have any tips or ideas in this area, please do let me know.

Thanks for taking the time to look. I'm all ears.

Removed previous attachment. See below for a revised version.

Thantos
11-29-2004, 03:29 PM
<pedantic>Your title is misleading. There is no such thing has unlimited in computers. Eventually you will run out of memory</pedantic>

Zach L.
11-29-2004, 03:31 PM
Bah! How can you dispute a claim like that Thantos? Now... give me all the digits of pi. :D

I'll look at it later. I'm curious what the result of the operator discussion was. :)

Sang-drax
11-29-2004, 04:11 PM
BUG 1: I tried to calculate 10.2 % 0.5 and it crashed.
BUG 2: I tried to calculate 2.4! and it crashed.

How do I calculate a fraction to more digits than the default?

Zach L.
11-29-2004, 05:11 PM
10.2 % 0.5?
That operation doesn't completely make sense from an algebraic standpoint.

11-29-2004, 05:21 PM
yes, modulus of non-integer values I thought was undefined?

Zach L.
11-29-2004, 05:29 PM
Well, mathematically, it is defined, but not very useful. The only two things you can take the modulus of in the rationals, Q (or the reals, R) are Q itself and 0, and
x = x (mod 0) [another way of writing x = x]
x = y (mod Q (or R)) [with a fixed x, this is satisfied by all y in the field]

I suppose you could make some operation to find out the integral number of times that one rational/real goes into another, but there doesn't seem to be anything particularly special about that operation.

Cheers

Thantos
11-29-2004, 06:32 PM
Well there is a fmod() so their floating number class should handle it.

LuckY
11-29-2004, 06:39 PM
Thanks for pointing out the bugs SangDrax. Interestingly (and annoyingly) enough the bug is of the simplest degree (just like the other bugs I found today). It is the simple > and < operators going out of range.

I've attached a revised version that correctly reports 10.2 % .5 = .2 and 2.4! = 3.36

You guys are right as far as I can tell about mod not being all that useful for floating point numbers, but since I was creating a floating point number class I felt it was absolutely necessary for whatever maniacs could find a reason to make good use of it; besides, I needed SangDrax to find something wrong. ;P

Thanks for checking it out. Again, any other bugs or suggestions are greatly appreciated.

I forgot that you wanted to be able to change decimal precision, SangDrax, but just remembered and included it (the library code was there but I was too lazy earlier to add the dialog functionality). Also, it will tell you how long your last calculation took.

Zach L.
11-29-2004, 06:42 PM
Whoaa.. That floating point mod is strange.

LuckY
11-29-2004, 07:04 PM
Why is the floating point mod strange? Hmm..

Zach L.
11-29-2004, 07:10 PM
It just doesn't fit the algebraic definition of modulus, so I don't know what it is doing. I would've expected it to be defined so that 10.2 % .5 = 20 (20 * .5 <= 10.2 < 21 * .5). [I take it fmod does exactly what your code does as well.]
At some point tonight, when I have time, I'm gonna open up that code and figure out what is going on. :)

LuckY
11-29-2004, 07:16 PM
Well, if you think about it it really isn't too strange. If you multiplied both of those operands by 10 it would be 102 % 5 = 2. In all actuality, it is very simply the remainder after a divide, so the result should be < the denominator (0.5). With this in mind, it doesn't seem to weird that the result would be 0.2, right?
Let me know what you come up with.

gcn_zelda
11-29-2004, 07:23 PM
I wish I knew what a modulus is.

Zach L.
11-29-2004, 07:32 PM
That works as a definition. I just wasn't sure what it was.

The one I am used to is as follows. You have a set with an addition operator and a multiplication operators such that there is a multiplicative identity in the set (1*a = a) and an additive identity (0+a=a). Additionally, both operations are commutative and associative, and addition is invertible (a in the set implies -a in the set so a+(-a)=0), but not necessarily so for multiplication. Call the set R.

So, an ideal is a subset p (called an ideal) such that within p, you have 0, and -a for each a in it. Additionally, R*p=p (any element of R times any in p yields one in p). And you take the modulus with respect to the ideal. So, x=y(mod p) means x-y is in p. For the integers, each ideal is generated by a single element (the element and its product with everything else), so you just write the x=y(mod n) where n is the generating integer.

For something that has multiplicative inverses for all of its non-zero elements, like the rationals or reals, then it turns out that the only primes are the one generated by 0 and the field itself (F).
So, x=y(mod 0) is x-y is some multiple of 0 and hence x=y
While x=y(mod F0 is x-y is in F, so the have no special relation to each other (particularly since this was required by the definition of F).

That was a long-winded explanation of why it didn't make sense to me. :) That said, the definition you gave makes much more sense than the one I proposed a post ago.

LuckY
11-29-2004, 07:42 PM
I wish I knew what a modulus is.
I sense your sarcasm, but, as you can tell from Zach's honesty, for some people there exists some ambiguity, or even an extreme lack of clarity. So, let's not be so condescending here, huh, ganon? Word.

gcn_zelda
11-29-2004, 08:26 PM
No, I'm serious. I don't know what a modulus is :P

Zach L.
11-29-2004, 08:40 PM
When in doubt, MathWorld! (http://mathworld.wolfram.com/Congruence.html) :cool:

gcn_zelda
11-29-2004, 08:46 PM
Maybe I'll just wait 'til Calculus or whenever we learn about those thingies.

linuxdude
11-29-2004, 09:17 PM
do you ever program. You need modulus in a lot of cases.

Sang-drax
11-30-2004, 10:32 AM
gcn_zelda:
To calculate 10.2 % .5 you could think of is as repeatedly adding/subtracting 0.5 from 10.2 until to recive a value within 0-0.5.

To calculate x % y, add or subtract y from x until you get a value in the interval [0,y[.

Question:
Why does (pi)! equal approx. 7.68 but
1.3! = 1
Are you using the gamma function or not?

EDIT: Matlab says that gamma(pi + 1) = (pi)! ~= 7.1881
1.3! ~= 1.1667
or
1.3! = 1 if you truncate

ober
11-30-2004, 10:47 AM
Bug: When calulating something that takes more than 1 second, you need to report it as such or just change it. I did Pi! and it took 7.xxx seconds, but it says 7.xxx msecs.

Other than that, good job. I didn't dig too hard, but it seems fairly solid.

Sang-drax
11-30-2004, 12:31 PM
Well, mathematically, it is defined, but not very useful. The only two things you can take the modulus of in the rationals, Q (or the reals, R) are Q itself and 0, and
x = x (mod 0) [another way of writing x = x]
x = y (mod Q (or R)) [with a fixed x, this is satisfied by all y in the field]

I'd say it isn't possible to use "mod 0". It doesn't make any sense.
The calculator answers, somewhat cryptically, 5 % 0 = 00

Zach L.
11-30-2004, 03:42 PM
It's a perfectly legal algebraic definition. The element 0 generates an ideal [called (0)] which consists of the element 0 alone. So, you can take the modulus with respect to it. You just need the cosets x+(0) and y+(0) to be identical, which only happens when x=y. See my earlier post that goes into this in more detail.

LuckY
11-30-2004, 11:36 PM
Question:
Why does (pi)! equal approx. 7.68 but
1.3! = 1
Are you using the gamma function or not?

EDIT: Matlab says that gamma(pi + 1) = (pi)! ~= 7.1881
1.3! ~= 1.1667
or
1.3! = 1 if you truncate
Pardon my ignorance, but I am unfamiliar with the "gamma function." Thank you for pointing out the mistake. The way I am now calculating factorial (and the way I should have been) is just the standard [n * (n - 1) * (n - 2) * ...]. So, using your example, 1.3! = 1.3 * 0.3 = 0.39. Before I was thinking in terms of integers and stopping at 1 instead of zero. What exactly is "the gamma function" and how does it yield 1.3! = 1.1667? Thanks for the info (the horse's mouth is great, but please don't point me at google).

Bug: When calulating something that takes more than 1 second, you need to report it as such or just change it. I did Pi! and it took 7.xxx seconds, but it says 7.xxx msecs.
Thanks for reporting. Actually it doesn't matter if it is over a second, the result is always in seconds, not milliseconds. It's fixed now. Out of curiosity, what are your specs? I just ran it on this machine (Centrino 1.5Ghz) and it took just over 10 seconds.

In doing more testing of my own I see that I am up against a foe that may conquer me and my measly HugeFloat class. Calculating a number to a floating point power means deadly problems. Currently my exponent function just treats the power like an integer because otherwise I am screwed since things to decimal powers mean strange things. It's possible that it will be a root (e.g., 1 ^ 0.5 = 1 ^ 1/2 = sqrt(1) = 1.4142135623730950488016887242097). If not, what exactly does a power to a floating point value mean? Should I just stick to treating it like an integer always? Should I always check if the exponent can legally be interpreted as a root? I don't know if I'm up for the challenge at this point... Stick a fork in me, I'm done.

Zach L.
12-01-2004, 12:09 AM

PJYelton
12-01-2004, 02:25 AM
>>If not, what exactly does a power to a floating point value mean?<<

If you convert the floating point to a fraction, then x^(y/z) is equal to z'th root of (x^y). For example 4^(2/3) is cube root of 4^2. So 4^(3.16529) is (4^3)*(4^(16529/100000)) which is 64 times the the 100000th root of 4^16529.

Not sure exactly how to do a non repeating infinite float though, such as 4^(pi).

Sang-drax
12-01-2004, 07:05 AM
Pardon my ignorance, but I am unfamiliar with the "gamma function."

The gamma function is defined as following:

G(x) = integral(0,inf) ( t^(x-1) * e^(-t) * dt )

Partial integration shows that G(x+1) = (x+1) * G(x). This combined with G(1) = 1 shows that the gamma function equals the factorial function for integers. The gamma function is a useful generalization of the factorial in some cases.

Calculating a number to a floating point power means deadly problems. [...] If not, what exactly does a power to a floating point value mean? Should I just stick to treating it like an integer always? Should I always check if the exponent can legally be interpreted as a root? I don't know if I'm up for the challenge at this point...

Not sure exactly how to do a non repeating infinite float though, such as 4^(pi).

Calculating a real number to the power of a rational number is well-defined. We can use this to define a real number to the power of an irrational:

a^b where a,b € R is defined as following:

lim{p/q -> b} a^(p/q)

i.e. b is approximated with a rational number which gets closer and closer to the actual real number.

Note that this means that raising a negative number to an irrational number is undefined. There's no way of determining the sign of the result.

This is not easy to implement, I can imagine. :)
I've only done a BigInt class, which is alot easier.

ober
12-02-2004, 09:43 AM
Thanks for reporting. Actually it doesn't matter if it is over a second, the result is always in seconds, not milliseconds. It's fixed now. Out of curiosity, what are your specs? I just ran it on this machine (Centrino 1.5Ghz) and it took just over 10 seconds.
2.8 GHz P IV with 512 RAM

LuckY
12-02-2004, 05:39 PM
Okay a much better operating version is available to anyone who is at all interested in using it or bug-running it. You can get it at http://www.geocities.com/lucky760/hugecalc. I made it dual-threaded so that the damn thing doesn't hang up while running long calculations. Also if it's taking too long for your liking you can just hit ESC or click the X to cancel the current computation. In addition to that I added tool tips to tell what things do and what their keyboard equivalents are. That's the other (and best) thing; you can enter values with your keyboard instead of having to mouse click every number. Finally you can cut/copy/paste stuff using CTRL+Z||C||V and SHIFT+INSERT||DELETE.

Sang-drax
12-02-2004, 06:23 PM
It gives incorrect results when raising, for example, pi to the power of pi. It would be better to just display an error message or something.
And now it seems to answer n % m = 0 if n<m

LuckY
12-02-2004, 10:10 PM
Geez I'm very thankful that you're checking this garbage out for me. Just as before it is a simple thing hardcoded in (if num is < denom on mod) and I was providing the wrong answer.

That is an excellent recommendation. I will absolutely inform the user of the lack of rational exponents. Thanks again sang-drax. I appreciate it.

LuckY
12-03-2004, 01:09 PM
Okay I made those changes I talked about and found another modulus problem (I didn't provide enough precision for the division when trying to calculate the mod) and fixed lots of other small things. It should be very sturdy now (just don't throw any rocks at it or it'll fall down). Thanks again Mr. Drax.

If you are at all interested in using it or bugtesting it, you can get it from that link in my sig. Thanks all.

Sang-drax
12-03-2004, 02:54 PM
Try dividing 1 with 3.141592. Gives quite different results if the precision is changed from 30 to 10.

Also, Ctrl+C doesn't work for me (WinXPprofSP2) Ctrl+X erases window but doesn't add anything to the clipboard.

LuckY
12-03-2004, 03:49 PM
I've made one more change regarding precision. I was taking into account the integer part of the number and doing some other things that cause me to miscalculate, but it is much better now. If you still have any difficulties in computation please let me know.

Hmm... The clipboard thing sounds very strange. I ran this on my other computer (also WinXP pro SP2) and didn't have any difficulties. I added a message on clipboard failure. If you give it a try and it still fails, please let me know what it says. On this (Win98) it works seamlessly.

Again, there is another version (1.02) at that link (here (http://www.geocities.com/lucky760/HugeCalc.zip)).

Sang-drax
12-04-2004, 05:40 AM
:confused: Hmm... no error message -- it just doesn't work.

LuckY
12-04-2004, 06:12 PM
Okay sang-drax, I've got you cornered. The clipboard operations are working perfectly now and I discovered a problem that you didn't. ;)
I tried 1/Pi to see how accurate it was to the 1/Pi button's value and only the first few digits were correct. After several hours of painstaking work I finally discovered I wasn't borrowing properly in my subtraction and that screwed up my division. Now I can confidently say that every operation is calculated flawlessly.

Seriously, thanks a whole lot.