Rounding issue: Sometimes rounding down instead of up for decimal values that end in 5 - rounding

After using a proprietary language for some time, I have noticed that some values when rounded to two places/three places, round incorrectly.
Examples: 4.055 rounding to 4.05, 2.495 rounding to 2.49
I understand that this may be an issue with the precision, but I am unsure as to how to predict when the rounding issue will occur. Any help appreciated.

Related

Excel rounds up number in one record, rounds down the same number in another

Excel is rounding numbers inconsistently that is causing me issues. When using ROUND(), sometimes it rounds a specific number up, while at other times it rounds the same value down.
I've tried setting Excel to show exact values in settings, but it doesn't change anything.
This is an example of what is happening.
This is the simple formula ROUND((A1-B1)/2,4)
For one record I have the values (.3159 - .3152) which evaluate to .0007 then divide by 2 to get .00035.
For the next record I have the values (.3554 - .3547) which also evaluates to .0007 and divided by 2 results in .00035
So, even though both values are .00035 when I round off to 4 decimal places I am getting .0003 for one and .0004 for another. Same number, rounding to the same number of places, two different results. How can I fix this?
This is an issue with floating point numbers that is inherent and cannot be solved, only avoided.
Try these tests in Excel:
=(0,3159-0,3152)=(0,3554-0,3547) gives you FALSE.
=(0,3159-0,3152)-(0,3554-0,3547) gives you something like 5.55112E-17.
If you cannot accept the differences, you should round already in the middle of the calculation, not only at the end:
=ROUND(0.3159-0.3152,4)=ROUND(0.3554-0.3547,4) is TRUE
=ROUND(0.3159-0.3152,4)-ROUND(0.3554-0.3547,4) is 0
further reading: Is floating point arithmetic stable? and Binary floating point and .NET, by highly regarded Jon Skeet.

EXCEL sumproduct formula not counting correctly

I have a formula in EXCEL 2013 that counts values where the decimal point = .16
=SUMPRODUCT(--(MOD(D2:D9,1)=0.16))
so for example 2.16, 15.16 will be a count of 2. However if the value is 32.16 or greater then will not count. This is very weird issue and cannot fathom this out.
You've hit a floating point error¹. The remainder reads as 0.159999999999997, not 0.16. Round it to at least four decimals to get an accurate read.
=SUMPRODUCT(--(ROUND(MOD(D2:D9,1), 4)=0.16))
¹ See 15 digit precision floating point errors and Floating-point arithmetic may give inaccurate results in Excel.

Round number wrong

A friend of mine discovered a really weird thing in MS Excel. Excel rounds down some specific numbers the wrong way, actually it rounds down a number that shouldn't need rounding.
As far as I have tested, it happens in most versions of MS Excel 2007+
Eg. the number 10358.165790 will be rounded down to 10358.1657899999.
Apparently it only happens in this interval: 8192.165790 - 65535.165790.
It is really weird - it doesn't happen with eg. .165890 or .165690, only with .165790.
Do any of you know why this happens and why it only accounts to certain numbers?
Excel uses an IEEE754 64 bit double precision floating point type to represent numeric data; with some clever formatting and roundup tricks to get sums like 1/3 + 1/3 + 1/3 correct.
What you are observing is a natural consequence of that numeric scheme only being accurate to 15 significant figures. Unless the number happens to be a dyadic rational, in which case it can be stored exactly, the closest representable number is chosen to the one you actually want. This may be below or above a rounding cutoff.
It will occur in other ranges other than the one you cite too.

Rounding Decimals to 0, 5, 1

Here in Kuwait (Arab country) we used three decimals after comma such as (100.523, 50.978, 340.143, 76.552, 414,081... etc).
How can rounding only the decimals to 0 or 5 or 1, for example:
1. 50.231 rounding to **50.230** Keeping same
2. 50.232 rounding to **50.230**
3. 50.233 rounding to **50.230**
4. 50.234 rounding to **50.230**
5. 50.235 rounding to **50.235** Keeping same
Another type of example for rounding to 1:
1. 50.236 rounding to **50.235**
2. 50.237 rounding to **50.235**
3. 50.238 rounding to **50.235**
4. 50.239 rounding to **50.235**
5. 50.240 rounding to **50.240** Keeping same.
If it possible can solve this issue in excel by using a formula or no?
You seem do be doing a FLOOR operation, not a round. The difference is that floor always rounds downwards, to the precision decided, while ROUND rounds to the nearest, either up or down.
With excel, you can do the following and set the precision:
=FLOOR(A1, 0.005)
For reference, see https://support.office.com/en-sg/article/FLOOR-function-14bb497c-24f2-4e04-b327-b0b4de5a8886
If using ROUND, your examples
3. 50.238 rounding to **50.235**
4. 50.239 rounding to **50.235**
Would instead round to 50.240 since that is the nearest multiple of 0.005.
You can use this:
=FLOOR.MATH(A1, 0.005)
or
=ROUNDDOWN(A1 * 200; 0) / 200
It looks like you are rounding down to the nearest 0.005. For that, try this formula, which gives the results you show in your example:
=ROUNDDOWN(A1/0.005,0)*0.005
Check that the formula also gives your desired results for negative numbers.
The FLOOR function would need to be modified to work for negative numbers in versions of Excel prior to 2010.
EDIT: In your Comment below, you have changed the results for Example 2. It now appears that you want to perform a simple ROUND, unless the value is nn.nn5 (e.g. exactly on the .005 boundary).
It is not clear what level of precision you require for this, and the IEEE standard for double-precision floating point arithmetic allows for a bit of "slop". So you will need to adjust the comparison exponent depending on your desired level of precision.
Here are the results, showing both your original Examples in your Question, and the different set of Examples in your Comment:

Are rounding errors possible in Excel if under significant figure limit?

I have a database which houses scaled integers, the longest being 10 digits long. I am attempting to convert these to decimal values in Excel, moving the decimal point left by 4 digits, i.e. dividing by 10000.
Given that these integers are currently under the 15-digit significant figure limit, and will remain so, is there a possibility that I can encounter rounding errors?
is there a possibility that I can encounter rounding errors?
Strictly speaking I think yes. For example:
but what may be significant is that the discrepancy as shown (all formatted the same, the smaller black ones created by formula, the red ones by difference of those immediately above) is in the tenth decimal place, so hopefully not a problem.

Resources