Rational numbers in .NET

  • Thread starter Thread starter Mike Friel
  • Start date Start date
M

Mike Friel

I am writing an assembly that will be used in a suite of
financial applcations. Naturally I must use integer
arithmetic to ensure the accuracy of my calculations and
in the past [in c++] have used the rational number class
from the Boost libararies. Is there anything out there
like that or for that matter in the framework that I am
missing.

Cheers

Mike
 
I'm not sure what your problem is

AFAIK, a "long" in .net is a 64-bit integer. Enough for most financial
applications surely?
 
Andy

Thanks for replying. To clarify... in financial
calculations there may be a requirement to resolve
complex mathematical formulae on what are ostensively
rational numbers (fractions) e.g. an exchange price from
a market feed. 4563.23 being involved in a calculation
with 10 other variables each with between 0 and 5 decimal
places. Clearly using floating point representations the
calculations here would result in dramatically inaccurate
answers as not all decimal fractions can be accurately
represented by a floating point number. This is where
rational numbers come in as the allow completely accurate
representations of fractions and support all the standard
mathematical operators.

As stated Boost have such a library
(http://www.boost.org/boost/rational.hpp) which I have
used in our proprietary trading system and it works a
treat so I was looking to source something like that to
save me the pain of hand rolling it.

BTW this is a templatized class and in practice I have
used int64 as the template parameter.

int64 on its own is not sufficient as the range
representated may not be wide enough in all cases or one
would have to may undue attention to the order of sub-
calculations to guarantee that overflow does not occur.

Hope this clarifies my question.

Cheers

Mike
-----Original Message-----
I'm not sure what your problem is

AFAIK, a "long" in .net is a 64-bit integer. Enough for most financial
applications surely?

Mike Friel said:
I am writing an assembly that will be used in a suite of
financial applcations. Naturally I must use integer
arithmetic to ensure the accuracy of my calculations and
in the past [in c++] have used the rational number class
from the Boost libararies. Is there anything out there
like that or for that matter in the framework that I am
missing.

Cheers

Mike


.
 
the Decimal class may be accurate enough....

Andy

Thanks for replying. To clarify... in financial
calculations there may be a requirement to resolve
complex mathematical formulae on what are ostensively
rational numbers (fractions) e.g. an exchange price from
a market feed. 4563.23 being involved in a calculation
with 10 other variables each with between 0 and 5 decimal
places. Clearly using floating point representations the
calculations here would result in dramatically inaccurate
answers as not all decimal fractions can be accurately
represented by a floating point number. This is where
rational numbers come in as the allow completely accurate
representations of fractions and support all the standard
mathematical operators.

As stated Boost have such a library
(http://www.boost.org/boost/rational.hpp) which I have
used in our proprietary trading system and it works a
treat so I was looking to source something like that to
save me the pain of hand rolling it.

BTW this is a templatized class and in practice I have
used int64 as the template parameter.

int64 on its own is not sufficient as the range
representated may not be wide enough in all cases or one
would have to may undue attention to the order of sub-
calculations to guarantee that overflow does not occur.

Hope this clarifies my question.

Cheers

Mike
-----Original Message-----
I'm not sure what your problem is

AFAIK, a "long" in .net is a 64-bit integer. Enough for most financial
applications surely?

Mike Friel said:
I am writing an assembly that will be used in a suite of
financial applcations. Naturally I must use integer
arithmetic to ensure the accuracy of my calculations and
in the past [in c++] have used the rational number class
from the Boost libararies. Is there anything out there
like that or for that matter in the framework that I am
missing.

Cheers

Mike


.
 
Mike,
There is no Rational number defined in .NET.

Have you looked at the System.Decimal data type, it is a scaled integer, so
it is able to accurately hold fractions.

Are you using C# or VB.NET? With C# you could define a Rational structure
that maintains both parts, and support Operator Overloading so it is easier
to work with. You will need to wait for Whidbey (VS.NET 2004) for Operator
Overloading in VB.NET.

Matthew MacDonald's book "Microsoft Visual Basic .NET Programmer's Cookbook"
from MS Press, has a sample Fraction class (no operator overloading) that
you could use as a starting point, to create a Rational class.

Hope this helps
Jay
 
Thanks, I understand what you are getting at now. Sorry I don't have an
answer but it's interesting to understand what you're doing.

It reminds me of a casio calculator I used to have at school that could do
fractions... happy memories :)

If you have to roll your own, you could maybe try and find a java version to
port - porting java to c# is usually v easy indeed.


Andy

Thanks for replying. To clarify... in financial
calculations there may be a requirement to resolve
complex mathematical formulae on what are ostensively
rational numbers (fractions) e.g. an exchange price from
a market feed. 4563.23 being involved in a calculation
with 10 other variables each with between 0 and 5 decimal
places. Clearly using floating point representations the
calculations here would result in dramatically inaccurate
answers as not all decimal fractions can be accurately
represented by a floating point number. This is where
rational numbers come in as the allow completely accurate
representations of fractions and support all the standard
mathematical operators.

As stated Boost have such a library
(http://www.boost.org/boost/rational.hpp) which I have
used in our proprietary trading system and it works a
treat so I was looking to source something like that to
save me the pain of hand rolling it.

BTW this is a templatized class and in practice I have
used int64 as the template parameter.

int64 on its own is not sufficient as the range
representated may not be wide enough in all cases or one
would have to may undue attention to the order of sub-
calculations to guarantee that overflow does not occur.

Hope this clarifies my question.

Cheers

Mike
-----Original Message-----
I'm not sure what your problem is

AFAIK, a "long" in .net is a 64-bit integer. Enough for most financial
applications surely?

Mike Friel said:
I am writing an assembly that will be used in a suite of
financial applcations. Naturally I must use integer
arithmetic to ensure the accuracy of my calculations and
in the past [in c++] have used the rational number class
from the Boost libararies. Is there anything out there
like that or for that matter in the framework that I am
missing.

Cheers

Mike


.
 
Back
Top