473,508 Members | 4,179 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rational numbers in .NET

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
6 2718
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
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
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
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
Thanks guys

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 21 '05 #6
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
2371
by: Mike Meyer | last post by:
PEP: XXX Title: A rational number module for Python Version: $Revision: 1.4 $ Last-Modified: $Date: 2003/09/22 04:51:50 $ Author: Mike Meyer <mwm@mired.org> Status: Draft Type: Staqndards...
20
2109
by: Mike Meyer | last post by:
This version includes the input from various and sundry people. Thanks to everyone who contributed. <mike PEP: XXX Title: A rational number module for Python Version: $Revision: 1.4 $...
2
2793
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
1
3458
by: lordhavemercy | last post by:
Rational Arithmetic I need a possible representstion of Rational numbers in C using Structures. It has to be a foating-point representatiobn. Each rational number has to represent/store the...
3
3362
BenTheMan
by: BenTheMan | last post by:
Hello all. I will probably be frequenting these discussions in the future. I am a graduate student in physics learning C++ on the fly. This is probably an easy quesiton, but my background in...
6
4630
by: penny | last post by:
In this assignment we shall look at a possible representation of rational numbers in java using objects. The java language represents rational numbers using the same representation used for other...
1
2844
by: ben kipkorir | last post by:
In this assignment we shall look at a possible representation of rational numbers in java using objects. The java language represents rational numbers using the same representation used for other...
135
4154
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
5
4612
by: anumliaqat | last post by:
hello!! i hav a problem.my program is giving correct outputs for some inputs but wrong results for others. for example if u give (4 2 2)&(2 1 2) to it,it shows all results correct....
0
7332
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7058
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.