473,799 Members | 2,941 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Incorrect comparison of two identical values rounded to 2dp

97 Recognized Expert New Member
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=Ro und([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 2398
ADezii
8,834 Recognized Expert 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=Ro und([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 Recognized Expert New Member
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 Recognized Expert 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!!
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 Recognized Expert New Member
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 Recognized Expert Expert
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(<expressio n>) 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 Recognized Expert New Member
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
1929
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. The implementation is really simple, but the key is to understand what the function should be. I'll post Perl and Python codes tomorrow for those interested. If you are a perl programer, try to code it in Python. (it's easy.)
46
5178
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 was told that wouldn't it be wise to first get the length of the strings..if it doesn't match then they are not the same..I agreed...then he said..but again that would be an overhead first measuring the length...and then doing a character by...
4
8837
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 to proceed. any guidance in this regard would be well appreciated. Thanx
32
4122
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 ); } int main() { std::deque<double> pt ;
43
2746
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 the other, an entire column is replaced. --------------------------------------------------------------------------------------- ''' An oddity in the behavior of lists of lists. Occurs under Python 2.4.3 (#69, Mar 29 2006, 17:35:34)
2
5124
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 promoted to "signed int": signed char
6
8602
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 circumstances(XML file source) and with exactly the same value it gets it perfectly correct all the time. These are the results I got, XML is always correct, CSV are only incorrect for some of the values (above about 0.01) but always gives the...
6
9873
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 different maps. I know how to copy the entire map to another: // Copy fset1 to fset3 std::copy(fset1.begin(),fset1.end(),std::inserter(fset3, fset3.begin()));
26
2904
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 double types: l_lnk1->lnk_freq == l_lnk2->lnk_freq Two real (float, double, or long double) values are compared directly using == or != primitive. This may produce unexpected results since
1
10222
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10026
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2938
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.