By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,933 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,933 IT Pros & Developers. It's quick & easy.

Rational numbers in .NET

P: n/a
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
Jul 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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" <mf****@starmark.biz> wrote in message
news:08****************************@phx.gbl...
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

Jul 21 '05 #2

P: n/a
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 financialapplications surely?

"Mike Friel" <mf****@starmark.biz> wrote in message
news:08****************************@phx.gbl...
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

.

Jul 21 '05 #3

P: n/a
Sam
the Decimal class may be accurate enough....

<an*******@discussions.microsoft.com> wrote in message
news:00****************************@phx.gbl...
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" <mf****@starmark.biz> wrote in message
news:08****************************@phx.gbl...
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

.

Jul 21 '05 #4

P: n/a
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

"Mike Friel" <mf****@starmark.biz> wrote in message
news:08****************************@phx.gbl...
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

Jul 21 '05 #5

P: n/a
Thanks guys

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 21 '05 #6

P: n/a
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.
<an*******@discussions.microsoft.com> wrote in message
news:00****************************@phx.gbl...
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" <mf****@starmark.biz> wrote in message
news:08****************************@phx.gbl...
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

.

Jul 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.