Hi,I think that would be hard in general, since doubles can represent irrationial numbers as well as rational numbers.Otherwise, you couldconvert the double to an int using the int or round functions and make a BigNum out of that.Kurt

By on 2/25/2009 2:58 PM ()

"doubles can represent irrational numbers"

???

How can this be? Isn't the (numeric, not symbolic) representation of a number in a computer necessarily finite, hence making the number rational?

Example:

PI -> Irrational

3.1415...[and six trillion decimal numbers more] -> Rational

Are there any good resources out there explaining what "irrational" means in this context?

By on 2/26/2009 9:41 AM ()

"doubles can represent irrational numbers"

???

How can this be? Isn't the (numeric, not symbolic) representation of a number in a computer necessarily finite, hence making the number rational?

Right. I should've said: doubles can represent approximations of irrational numbers, as well as rationial numbers. There might be a way in which you could re-construct precise rational numbers from an approximation of a rational number, but it is by definition impossible to extract a precise rational number from (an approximation of) an irrational number like PI. It is probably impossible to distinguish the two cases.Anyway, you're trying to fit a square peg in a round hole.Kurt

By on 2/27/2009 1:35 AM ()

So, based on the comments above and some detective work: in summary, BigNum is a type to store non-integers with arbitrary precision, right?

By on 2/27/2009 10:32 AM ()

Good point. Seems like there should be a way to create a BigNum (rational number) from any floating point number in the .Net type system.

I did find an approach that works (using the number 2.75 as an example):

  1. Get the integral and decimal portions of your floating point number separately (2 and 75).
  2. Express the decimal portion as a fraction in a string ("75/100").
  3. Convert the decimal portion to BigNum using BigNum.Parse, passing in the string from step 2.
  4. Convert the integral portion to BigNum using BigNum.FromInt (or from BigInt if necessary).
  5. Add the two big nums together to get the final BigNum result (represented as 275/100).

This was more work than I was hoping to have to do myself but it appears to be solid (do let me know if anyone sees a flaw in this approach).

Thanks

By on 2/26/2009 10:08 AM ()

I suppose the really good way to do this is to use mantissa and exponent from the floating point representation. However, what will be the result of:

1
2
let float_to_bignum x = // [some magic]
1.0 / 3.0 |> float_to_bignum

It will most definitely not be 1/3N! This raises two questions:

  1. What purpose is it to convert to BigNum if you already lost precision?
  2. Or should the conversion find the rational with least denominator that matches the desired precision?

The latter seems like an interesting number-theoretic problem. Any takers? [;)]

By on 2/26/2009 11:21 PM ()

<quote> 1.0 / 3.0 |> float_to_bignum </code> It will most definitely not be 1/3N! This raises two questions: <OL><li>What purpose is it to convert to BigNum if you already lost precision? </li><li>Or should the conversion find the rational with least denominator that matches the desired precision?</LI></OL> The latter seems like an interesting number-theoretic problem. Any takers? [;)] </quote> #2 does sound interesting - there are some efficient ways to get fractions out of decimals using continued fractions, but I can't convince myself they would get the smallest denominator. In fact, I can convince myself they won't, if the desired precision is goofy. For example, if your desired precision is .1, and your decimal is .25, you're going to get 1/4 even though you want 1/3. The best I can think of is brute force.

By on 2/27/2009 4:43 PM ()

Bah. I'm not interested anymore. Imagine if there were a type called "transcendent"; that'd be cool, right?

But let's not try to square the circle as Kurt suggests; instead, let's just be stupid, not question anything, live happily with 1/2 = 0 and the fact that "double" also represents "irrational" numbers...

By on 2/27/2009 8:58 PM ()

Hmm. Seems my responses came over differently than I intented.Maybe what you're looking for is arbitrary precision math, e.g. [link:www.extremeoptimization.com] has a BigInt, BigRatinal and BigFloat. But afaik the math libraries that come with F# do not cater for that.Also if you want to know more about floating point math, I recommend: [link:www.extremeoptimization.com] there, please do question anything and everything.Kurt

By on 3/1/2009 3:15 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper