6

I have values greater than 1.97626258336499E-323

I cant use BigInteger also as it handler only integer values

Any help is appreciated

Here is the code that failed also failed with some solution given by some users:

BigValue / (Math.Pow((1 + ret), j));

WHere BigValue is something like 15000.25

ret is -0.99197104212554987

And j will go to around 500-600.

I am not gettting how to use Rational Class for this too

Moons
  • 3,833
  • 4
  • 49
  • 82
  • According to documentation the max vaue is 1.7976931348623157E+308. http://msdn.microsoft.com/en-us/library/system.double.maxvalue.aspx – Richard Schneider Jan 23 '13 at 05:12
  • @RichardSchneider Pleaser read about Epsilon and my question is to store more bigger values for financial equations – Moons Jan 23 '13 at 05:13
  • I know of no `BigDecimal/Float` on .NET. You will have to resort to native libraries. – leppie Jan 23 '13 at 05:14
  • 1
    C# only has BigInteger built it (in .NET framework 4). Is decimal enough precision for your task? It's a 128-bit number that can hold values in the range ±1.0 × 10−28 to ±7.9 × 1028. – Niranjan Singh Jan 23 '13 at 05:16
  • @leppie Can you please advise some native libraries – Moons Jan 23 '13 at 05:17
  • @NiranjanKala I have case like Number/(1-0.9999999)power 400 etc.. In this case BigInteger becomes useless. Big Integer truncates values after the decimal – Moons Jan 23 '13 at 05:18
  • @Moons: go through [this](http://stackoverflow.com/questions/2863388/what-is-the-equivalent-of-the-java-bigdecimal-class-in-c) SO thread which may help you.. – Niranjan Singh Jan 23 '13 at 05:22
  • 1
    @Moons Not really. Just have two big integers, one that's a very big (integer) number, and another that's a power of some base. You just need to figure out how to perform the operations on these pairs of numbers based on what you need to support (i.e. addition, multiplication, etc.). – Servy Jan 23 '13 at 05:25
  • @Moons: Was thinking of GMP (and MPFR). – leppie Jan 23 '13 at 05:30
  • @Servy: While that will work, it will be slow and use too much memory (as you are representing the number exactly). – leppie Jan 23 '13 at 05:31
  • @leppie Yes i think GMP may be helpful.. – Moons Jan 23 '13 at 05:32
  • @leppie It may not be practical, or worth doing, it's just that the OP said it would be useless. It wouldn't be, it would just be inefficient and difficult. That's why it was a comment, not an answer. – Servy Jan 23 '13 at 05:33
  • @leppie, side note: I don't think any library that preserve 300+ digits in a number will be able to store values with significantly less overhead than BigInteger (I'd be surprised for even 2x difference). Also not sure what OP's requirement is since "grater than 1E-323" is not very big number... – Alexei Levenkov Jan 23 '13 at 05:47

5 Answers5

1

BigRational from the base class library team from Microsoft. It uses big integers to store it as a fraction, but supports all kinds of operators.

When it comes to printing it as a decimal, I think you need to write your own implementation for that. I have one written somewhere for this class, but I'd have to find it.

Christopher Currens
  • 29,917
  • 5
  • 57
  • 77
1

Here is something that may be useful. I used it a while back with no problem. It is a .Net BigDecimal class, you can download it from codeplex(or just look at the source):

http://bigdecimal.codeplex.com/releases/view/44790

It is written in VB.Net (.Net 4.0), but that shouldn't matter.

An example of its use in C#: http://www.dreamincode.net/forums/blog/217/entry-2522-the-madman-scribblings/

0

You will have to switch languages to one that has a BigFloat type (e.g. Haskel and Python have native packages) or else find a third party big float library with a C# binding. There was some discussion of such a binding for GNU MP, but it faded away. Maybe you'll write one!

See also this discussion where MS BigRational is discussed. However this is different from BigFloat.

Community
  • 1
  • 1
Gene
  • 46,253
  • 4
  • 58
  • 96
0

One solution might be to do your problems in log space instead.

your example would become:

exp(log(Number) - log(1-0.9999999) * 400)
wich
  • 16,709
  • 6
  • 47
  • 72
0

Learn how to use logs to work with numbers like these. Yes, you CAN use a big float package, but that is overkill almost always. You can usually get what you need using logs.