473,387 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Incorrect comparison of two identical values rounded to 2dp

97 Expert
Hi
I have a report which splits invoice costs to various departments and shows a summary.
The various divisors and multipliers used in deriving the values for each department, are such that the final figures will not be to 2 decimal places.
I decided to use the Round() function so that the calculated figure would equal the formatted figure. I then Summed the rounded figures to reach a total.
Occasionally the sum will not equal the actual invoice total, because of rounding errors.

I also print the actual invoice total on the report in a subreport.
To indicate that the values are different I have a conditionally visible label, warning of the error. However, more times than not, the label is visible even when the invoice value and report sum are identical.

Using the VB debug tracing/watch facility I can see that the values are indeed identical but the comparison evaluates to false.

Does anyone have any ideas how to get around this problem?

Report Detail Text box:
Split value rationalised=Round([Split value],2)

Report Footer Text box:
Total=Sum(Round([Split value],2))

Footer OnFormat:
Expand|Select|Wrap|Line Numbers
  1.     If Me.Total <> Me.Invoice.Report![total cost] Then
  2.         Me.Warning.Visible = True
  3.     Else
  4.         Me.Warning.Visible = False
  5.     End If
  6.  
Jun 13 '07 #1
6 2383
ADezii
8,834 Expert 8TB
Hi
I have a report which splits invoice costs to various departments and shows a summary.
The various divisors and multipliers used in deriving the values for each department, are such that the final figures will not be to 2 decimal places.
I decided to use the Round() function so that the calculated figure would equal the formatted figure. I then Summed the rounded figures to reach a total.
Occasionally the sum will not equal the actual invoice total, because of rounding errors.

I also print the actual invoice total on the report in a subreport.
To indicate that the values are different I have a conditionally visible label, warning of the error. However, more times than not, the label is visible even when the invoice value and report sum are identical.

Using the VB debug tracing/watch facility I can see that the values are indeed identical but the comparison evaluates to false.

Does anyone have any ideas how to get around this problem?

Report Detail Text box:
Split value rationalised=Round([Split value],2)

Report Footer Text box:
Total=Sum(Round([Split value],2))

Footer OnFormat:
Expand|Select|Wrap|Line Numbers
  1.     If Me.Total <> Me.Invoice.Report![total cost] Then
  2.         Me.Warning.Visible = True
  3.     Else
  4.         Me.Warning.Visible = False
  5.     End If
  6.  
Why not Round at all. How about truncating from the 3rd decimal position on such as:
Expand|Select|Wrap|Line Numbers
  1. Fix(((2987.367894)*100))/100 ==> produces 2987.36
Jun 13 '07 #2
kepston
97 Expert
Thanks for that.
Unfortunately for me, that produced inaccuracies everytime.
But it did give me an idea
Expand|Select|Wrap|Line Numbers
  1. Function Precise(num As Double, dp As Integer) As Double
  2.     ' This function takes a number (num) with an undefined number of decimal places
  3.     ' and returns the value with dp decimal places 
  4.     ' It is used for checking, by removing tiny errors eg 1.4e-14
  5.  
  6.     Dim pw As Integer
  7.  
  8.     pw = 10 ^ dp
  9.     Precise = int(num * pw)/pw
  10.  
  11. End Function
Initially this worked, but after testing with several data sets I saw that
Expand|Select|Wrap|Line Numbers
  1.  Precise(136.46,2)
returned 136.45!

Any form of division introduces errors.
So what I've come up with that works so far is this less than perfect routine
Expand|Select|Wrap|Line Numbers
  1. Function Precise(num As Double, dp As Integer) As Long
  2.     ' This function takes a number (num) with an undefined number of decimal places
  3.     ' and returns the value without decimal places left shifted to retain the required
  4.     ' accuracy
  5.     ' It is used for checking only, by removing tiny errors eg 1.4e-14
  6.  
  7. Dim pw As Integer
  8.  
  9.     pw = 10 ^ dp
  10.     Precise = num * pw
  11.  
  12. End Function
  13.  
And there was me, thinking that computers were good at numbers!!
Jun 15 '07 #3
ADezii
8,834 Expert 8TB
Thanks for that.
Unfortunately for me, that produced inaccuracies everytime.
But it did give me an idea
Expand|Select|Wrap|Line Numbers
  1. Function Precise(num As Double, dp As Integer) As Double
  2.     ' This function takes a number (num) with an undefined number of decimal places
  3.     ' and returns the value with dp decimal places 
  4.     ' It is used for checking, by removing tiny errors eg 1.4e-14
  5.  
  6.     Dim pw As Integer
  7.  
  8.     pw = 10 ^ dp
  9.     Precise = int(num * pw)/pw
  10.  
  11. End Function
Initially this worked, but after testing with several data sets I saw that
Expand|Select|Wrap|Line Numbers
  1.  Precise(136.46,2)
returned 136.45!

Any form of division introduces errors.
So what I've come up with that works so far is this less than perfect routine
Expand|Select|Wrap|Line Numbers
  1. Function Precise(num As Double, dp As Integer) As Long
  2.     ' This function takes a number (num) with an undefined number of decimal places
  3.     ' and returns the value without decimal places left shifted to retain the required
  4.     ' accuracy
  5.     ' It is used for checking only, by removing tiny errors eg 1.4e-14
  6.  
  7. Dim pw As Integer
  8.  
  9.     pw = 10 ^ dp
  10.     Precise = num * pw
  11.  
  12. End Function
  13.  
And there was me, thinking that computers were good at numbers!!
You stated that your data involved various costs. Have you tried using the Currency Data type? The Currency data type is useful for calculations involving money and for fixed-point calculations in which accuracy is particularly important.
Jun 15 '07 #4
kepston
97 Expert
You stated that your data involved various costs. Have you tried using the Currency Data type? The Currency data type is useful for calculations involving money and for fixed-point calculations in which accuracy is particularly important.
Thank you once again.
When I tried it, I found that it made no difference, until I changed the multiplying percentage field to Currency as well - not quite so intuitive!

The important fact to remember, then, is fixed point rather than floating point arithmetic.
Is this only available with the Currency data type?
I can see that in VB a variable can be declared as Currency, but can Currency data type be forced, for example, in QBE grid queries? Or is this unnecessary?
Jun 18 '07 #5
ADezii
8,834 Expert 8TB
Thank you once again.
When I tried it, I found that it made no difference, until I changed the multiplying percentage field to Currency as well - not quite so intuitive!

The important fact to remember, then, is fixed point rather than floating point arithmetic.
Is this only available with the Currency data type?
I can see that in VB a variable can be declared as Currency, but can Currency data type be forced, for example, in QBE grid queries? Or is this unnecessary?
CCur(<expression>) will coerce an Expression to the Currency Data Type. Several examples of CCur() can be seen in a few Northwind.mdb Queries, namely: Invoice Query, Order Details Extended, and Order Subtotals. I do believe, however, that the logic for using this Function is not the same as yours.
Jun 18 '07 #6
kepston
97 Expert
Thanks, I don't know how I missed CCur() when I searched Help, before my last post - unless I put CCurr.
I don't have the Northwind sample installed, I will check it out when I find my CD.
Jun 21 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Xah Lee | last post by:
here's a interesting real-world algoritm to have fun with. attached below is the Perl documentation that i wrote for a function called "reduce", which is really the heart of a larger software. ...
46
by: yadurajj | last post by:
Hello i am newbie trying to learn C..I need to know about string comparisons in C, without using a library function,...recently I was asked this in an interview..I can write a small program but I...
4
by: AJ | last post by:
i am making a small application, which acts like a Messenger sort of thing. As a part of it, there is a small module in which i need to implement an image comparison. am not quite sure of how...
32
by: ma740988 | last post by:
template <class T> inline bool isEqual( const T& a, const T& b, const T epsilon = std::numeric_limits<T>::epsilon() ) { const T diff = a - b; return ( diff <= epsilon ) && ( diff >= -epsilon );...
43
by: michael.f.ellis | last post by:
The following script puzzles me. It creates two nested lists that compare identically. After identical element assignments, the lists are different. In one case, a single element is replaced. In...
2
by: Frederick Gotham | last post by:
I just want to clarify my understanding of arithmetic and comparison between two different integer types. Phase (1): Integer Promotion ---------- All of the following types always get...
6
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar...
6
by: Evyn | last post by:
Hi, How do I compare 2 maps for identical keys, and if they have identical keys, check if they have identical values? In either case I want to copy ONLY that key value pair to one of two...
26
by: Pietro Cerutti | last post by:
Hi group, I always thought that applying a binary operator such as ==, !=, <= or well defined. Now, I'm passing a program through splint and it says: Dangerous equality comparison involving...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.