image: query screen
This query multiplies the value by 3,000 and then rounds the answer to three decimal places
R E L A T E D   C O N T E N T
ADVERTISEMENT

Hands on: A fraction more on fractions

We bring you a distillation of fractional arithmetic

Mark Whitehorn, Personal Computer World 18 Jul 2007
ADVERTISEMENT

I received an email from John Palmer who had a problem with fractions.

In his database, fractions are also represented as decimals – you can show the user the fraction but do the calculation on the decimal component.

Unfortunately, his data contains a large number of fractions which cannot be represented with complete accuracy in decimal format (thirds, for example). He asks whether these can be totalled accurately.

To illustrate the problem, imagine that we represent 1/3 as 0.33. If we then add the number to itself three times (0.33 + 0.33 + 0.33) we get 0.99. The correct answer is, of course, 1.0. We can try to represent 1/3 more accurately as 0.333, whereupon we get closer to the correct answer (0.999), but we never achieve total accuracy.

So, what can we do about fractions such as these? Well, take our two different answers of 0.99 and 0.999, respectively. We can try applying a rounding function. In Access (and most database engines) this function takes two arguments: one is the number to be rounded; the other is the number of decimal places to which the rounding is applied.

Round (0.99,2) returns 0.99 and round (0.999,3) returns 0.999. Crucially, however, round (0.999,2) returns 1.0. So we know it is possible to return the right answer, depending on the circumstances. The inevitable next question is, ‘What circumstances?’

It turns out that getting the correct answer depends on several factors:

  • The number of decimal places you start with.
  • The number of addition/subtraction operations you want to perform.
  • The accuracy you ultimately require.

To test this I built a table containing 10 different decimal representations of 1/3. I then ran a query against it that multiplies every row by 3,000 (ie, performs 3,000 additions) then rounds the answer to three decimal places. If I start with seven decimal places or more, perform 3,000 additions and round to three decimal places, it yields the correct answer.

You can play with the sample database yourself (If you’ve bought the DVDedition of PCW you’ll find the database DBCAUG07.MDB on the disc. It’s also on our website.) but if, for example, you chose to round to four decimal places, then you need to start with at least eight decimal places. If you then also perform, say, 9,000 additions, then eight still appears to be fine, but 30,000 additions now require starting with nine decimal places.

This kind of test can give you a feel for the limits within which you can work. For example, if we reduce the number of operations to 1,000, we get answers such as 3333.333. Is this really the same as 3,333 and 1/3? No, but it’s close and may be close enough for your needs. In other words, using a rounding function can help considerably with whole numbers.


All Software Applications
Tags: Databases

Like this story? Spread the news by clicking below:

Post this to Delicious del.icio.us    Post this to Digg Digg this    Post this to reddit reddit!

Permalink for this story
R E A D E R   C O M M E N T S
M A R K E T P L A C E
Get your free demo of Numara Track-It! 8 - the leading help desk solution for IT related issues.
Make presentations, review documents & share your entire desktop. 30-day free trial! (cc required).
Discover how remote support can fuel your IT business in ways you've never thought of before.
Apply ITIL best practices at your service desk while eliminating integration cost. Learn more here.
WAN based, automated, daily vulnerability assessments. Click here to try and request our whitepapers.
Have your product or service listed here >   
Sponsored links
F E A T U R E D   J O B S
Leek Wootton, United Kingdom | Warwickshire Police
 IT Business Analyst - Leek Wootton, Warwickshire - £29,112 - £31,491 PA - 37 hrs per week   Everyone who works for Warwickshire Police helps to protect our communities from harm. Work with us and ... more >
London, United Kingdom | British Museum
Senior Programmer - The British Museum - £40k+ - London   Although steeped in history, the British Museum is constantly striving to improve access to and understanding of one of the world's most diverse collections of antiquities from cultures ... more >
Reading, Berkshire, United Kingdom | EDS
Job Title Netcool Designer / Engineer Location Reading Short Description: DII The DII project is contracted to supply both hardware and software infrastructure solutions to support the MoD transition to a common base solution, based ... more >
Guildford, Surrey, United Kingdom | Enstar
 IT Development Manager/IT Development Project manager - Guildford - £40k - £60 plus benefits   Enstar (EU) Limited (formerly Castlewood (EU) Limited) is seeking an IT Development Project Manager and an IT Development Manager to ... more >
More job opportunities