VBA addition of doubles bug

guyhjs

New Member
Joined
Feb 16, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have a weird bug I cannot work through.


(3.1 + 0.1 = 3.2) = TRUE
(3.2 + 0.1 = 3.3) = FALSE

Please can someone explain why? This is messing with a while loop I have.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hi @guyhjs - welcome. It would be helpful to be able to see the entire line(s) of code, and any Dim statements if variables are involved.
 
Upvote 0
Okay, it's the old binary representation of decimal numbers thing. Is this any help?
(cdec(3.1) + cdec(0.1) = cdec(3.2)) = TRUE
(cdec(3.2) + cdec(0.1) = cdec(3.3)) = TRUE
 
Upvote 1
Hi CephasOZ. Thanks for your help this worked. It was a floating point error.
 
Upvote 0
Upvote 0
@Joe4. The article uses the Excel rounding and not the VBA rounding. I am not sure if bankers rounding doesn't come into play if you use VBA rounding. WorksheetFunction.Round would be safer.
 
Upvote 0
@Joe4. The article uses the Excel rounding and not the VBA rounding. I am not sure if bankers rounding doesn't come into play if you use VBA rounding. WorksheetFunction.Round would be safer.
Ah, good catch! I was focusing on the question, and hadn't noticed the "VBA" in the title.
 
Upvote 0

Forum statistics

Threads
1,216,067
Messages
6,128,590
Members
449,461
Latest member
jaxstraww1

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top