PC Review


Reply
Thread Tools Rate Thread

Checking if the sum of a percentage is equal to one

 
 
PSmith
Guest
Posts: n/a
 
      11th Jul 2008
Have a bit of a problem here.

Here's a sample sheet where I have reproduced my problem:
http://pbsmith.iweb.bsu.edu/percentageSum.xls (you will need have macros
enabled, as this is a VBA subroutine)

I have 2 columns, where the individual percentages are added together. I
have a "Grand total" which adds up the individual totals. I do a check
against the grand total to make sure it's equal to one (using .Range() <> 1)

The problem is if there are 10 values of 10%, this check fails.

If you replace one of the 10% values with two 5% values, the check succeeds
(as it should, as long as it still adds up to 100%).

I'm using Excel 2007. Any feedback on this problem would be greatly
appreciated; I'm at a bit of a loss myself.
 
Reply With Quote
 
 
 
 
Nigel
Guest
Posts: n/a
 
      11th Jul 2008
floating point arithmetic is the issue -- take a look here

http://support.microsoft.com/kb/78113


--

Regards,
Nigel
(E-Mail Removed)



"PSmith" <(E-Mail Removed)> wrote in message
news:ABEEBFAD-3D80-47FC-A198-(E-Mail Removed)...
> Have a bit of a problem here.
>
> Here's a sample sheet where I have reproduced my problem:
> http://pbsmith.iweb.bsu.edu/percentageSum.xls (you will need have macros
> enabled, as this is a VBA subroutine)
>
> I have 2 columns, where the individual percentages are added together. I
> have a "Grand total" which adds up the individual totals. I do a check
> against the grand total to make sure it's equal to one (using .Range() <>
> 1)
>
> The problem is if there are 10 values of 10%, this check fails.
>
> If you replace one of the 10% values with two 5% values, the check
> succeeds
> (as it should, as long as it still adds up to 100%).
>
> I'm using Excel 2007. Any feedback on this problem would be greatly
> appreciated; I'm at a bit of a loss myself.


 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      11th Jul 2008
One way to workaround that floating point problem

Sub test()
Dim i As Long, a As Double
Const FP As Double = 0.000000000000001 ' 1E-14

For i = 1 To 10
a = a + 0.1
Next

Debug.Print a = 1
Debug.Print Abs(a - 1) < FP

End Sub

Increase the constant to whatever value is close enough to zero for your
purposes., eg 1e-8

Regards,
Peter T

"PSmith" <(E-Mail Removed)> wrote in message
news:ABEEBFAD-3D80-47FC-A198-(E-Mail Removed)...
> Have a bit of a problem here.
>
> Here's a sample sheet where I have reproduced my problem:
> http://pbsmith.iweb.bsu.edu/percentageSum.xls (you will need have macros
> enabled, as this is a VBA subroutine)
>
> I have 2 columns, where the individual percentages are added together. I
> have a "Grand total" which adds up the individual totals. I do a check
> against the grand total to make sure it's equal to one (using .Range() <>

1)
>
> The problem is if there are 10 values of 10%, this check fails.
>
> If you replace one of the 10% values with two 5% values, the check

succeeds
> (as it should, as long as it still adds up to 100%).
>
> I'm using Excel 2007. Any feedback on this problem would be greatly
> appreciated; I'm at a bit of a loss myself.



 
Reply With Quote
 
PSmith
Guest
Posts: n/a
 
      11th Jul 2008
Excellent, thank you!

"Nigel" wrote:

> floating point arithmetic is the issue -- take a look here
>
> http://support.microsoft.com/kb/78113
>
>
> --
>
> Regards,
> Nigel
> (E-Mail Removed)
>
>
>
> "PSmith" <(E-Mail Removed)> wrote in message
> news:ABEEBFAD-3D80-47FC-A198-(E-Mail Removed)...
> > Have a bit of a problem here.
> >
> > Here's a sample sheet where I have reproduced my problem:
> > http://pbsmith.iweb.bsu.edu/percentageSum.xls (you will need have macros
> > enabled, as this is a VBA subroutine)
> >
> > I have 2 columns, where the individual percentages are added together. I
> > have a "Grand total" which adds up the individual totals. I do a check
> > against the grand total to make sure it's equal to one (using .Range() <>
> > 1)
> >
> > The problem is if there are 10 values of 10%, this check fails.
> >
> > If you replace one of the 10% values with two 5% values, the check
> > succeeds
> > (as it should, as long as it still adds up to 100%).
> >
> > I'm using Excel 2007. Any feedback on this problem would be greatly
> > appreciated; I'm at a bit of a loss myself.

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to get a text/letters to equal to a percentage jose l. Microsoft Excel Worksheet Functions 5 1st Jun 2009 02:10 PM
Checking equal dictionary Luigi Z Microsoft C# .NET 2 5th Jan 2009 03:37 PM
checking if two arrays are equal =?Utf-8?B?S25veA==?= Microsoft Excel Programming 3 1st Jun 2006 05:56 PM
Re: Checking if not equal Kaak Microsoft Excel Programming 0 27th Jun 2005 10:03 AM
Checking if not equal jpizzle Microsoft Excel Programming 1 27th Jun 2005 10:00 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:50 AM.