Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

floor w/ and w/o units gives different results

Status
Not open for further replies.

Jwouters

Mechanical
Feb 19, 2009
24
0
0
NL
Hi all, I'm pretty new to Mathcad (I'm still young ;)), but I have run across a pretty weird problem. I did try google/help/eng-tips without any luck.

I think my problem is best explained in the attached file but here it is:

My 'goal' is to floor a division incluing units m/mm. In my example, I divide 12.75m by 1275mm which should result in 10. But Mathcad actually return 9.99....998. 'flooring' this number would result in 9, where 10 is expected.

When I try to floor(12750/1275) I get 10, as expected.

I would like to know if there is any solution to this problem or if I'm just stuck with this error.
 
Replies continue below

Recommended for you

It's not an "error," i.e., it's a fundamental limitation of using binary floating point to represent decimal numbers. Your examples are not equivalent calculations.

TTFN
faq731-376
7ofakss
 
Again, the with-units and without-units calculations are not equivalent. Mathcad casts all quantities with units into an internal SI representation. Nonetheless, if you divide 12.75 by 1275*10^-3, you'll get something less than 10, and will do so for any variation where the exponent is nonzero.

BTW, my help for floor(z) states "greatest integer <=z"

TTFN
faq731-376
7ofakss
 
AS IRstuff says, when you create expressions with units, Mathcad converts all units into their SI equivalents (as floating point numbers), and it also converts all numeric terms into floating point numbers (whether written as integers or floating point numbers). Thus the expression which is entered as:

(12.75 m) / (1275 mm)

is actually calculated internally as something like:

(12.75 * 1.0) / (1275.0 * (1.0 / 1000.0))

We human beings are "clever" enough to see that this is exactly equal to 10, but Mathcad evaluates this expression as being "equal" to 9.999999999999998 - which is the answer it gives for the expression with units. (Try it!)

 
jhardy,

That's the result I get in Mathcad. The same computer gives a different result for a similar calculation in Excel. If I format the cell for the maximum 30 decimal places, I get 10 followed by 30 zeros. Any idea how Microsoft gets around the binary floating point limitation?
 
@stevenal,

Sorry! I can't help you with explaining why Excel and Mathcad get slightly different results in the 15th decimal place ...

... Other than to say they are programmed by different teams, using different compilers and different algorithms. (In fact, it would be quite remarkable if two different programs gave identical results to 15 decimal places for all mathematical floating-point expressions, given the accuracy constraints of representing real numbers in binary format!)

 
Actually it's more surprising that they don't give the same result since both just call on the division function of the processor rather than each coding division within the programs.
 
While the floating point processor instructions might be the same at the assembly language level, the internal representation of numbers are not the same. Mathcad's and Excel's format handlings are different. Mathcad's highest input exponent is 307 and Excel's is 308, while the lowest is identical (-307). While Mathcad can round up to 10^308, you cannot enter that value directly, nor can you get there formulaically. However, Excel allows numbers up to 1.7976931E308 to be entered.

TTFN
faq731-376
7ofakss
 
OK, that was only valid for M11, it looks like M14 and up have been modified to allow the same maximum value of 1.79769313485999*10^308.

Nonetheless, the residual from the original question is 2^-49, which is 3 bits off from the 52-bit fractional part of a double precision float.

TTFN
faq731-376
7ofakss
 
@davidbeach:
What makes you think that Mathcad and Excel would carry out EXACTLY the same sequence of calculations & function calls with EXACTLY the same binary representations for ALL terms in PRECISELY the same order?

Excel has no explicit means of representing units; in my example, I suggested that Mathcad MIGHT represent "1 mm" as "(1.0 / 1000.0) times 1 metre"; but it MIGHT store it as "0.001 metres"; or it MIGHT (probably!) use some other internal algorithm to handle the units.

I have no idea whether Mathcad does some sort of "symbolic shuffling" to determine the units of the solution, or if it multiplies and divides its internal representations of units (nor the sequence of such "cancelling" operations), or what. Bearing in mind the limits of precision of binary floating point maths, "0.001" and "(1.0 / 1000.0)" will not necessarily yield EXACTLY the same result carried across to the last available binary digit. Thus, multiplying by 0.001 or dividing by 1000.0 will not necessarily yield exactly the same result, because you are actually calling different processor functions (possibly in a different sequence of operations), using different stored binary numbers.

 
When I made my comparison, I used the unitless (12.75 * 1.0) / (1275.0 * (1.0 / 1000.0)) expression in both Mcad and Excel.

I was also under the impression that math was more about the processessor than the higher level coding, explaining the pentium bug that affected all programs in the same way as I recall.

Good discussion here. Never really questioned floating point math since abandoning my slide rule. Thanks for the education.
 
BTW: "If I format the cell for the maximum 30 decimal places, I get 10 followed by 30 zeros" is an illusion.

Do a "=pi()^2" and format the cell for 30 decimal places, and you'll see 16 zeros at the end, even though there are ostensibly a potential for 28 decimal places of precision.

However, Mathcad does have the capability of doing substantially high precision using its symbolic math processor, but that ptocessor is not unit-friendly.

If you're willing to mess up the units system a bit by redefining mm=0.001m, the symbolic processor will give you the correct numerical answer to 12.75m/1275mm, to whatever precision you want, and assign the answer to a variable. you could then use the numerical processor for the floor function on that variable.

TTFN
faq731-376
7ofakss
 
Sorry for my late response, but thanks for the answers/discussion.

I understand that software has to be programmed in certain ways (I have written some 'software' myself), but don't 'limitations' like the one discussed, just defeat the purpose of Math-software in some way?

If I wouldn't have used the floor function I would haven't have had a single problem with 10 actually being 9,99...98, but in this specific case it could actually be make-or-break. So is the lesson learned from this that you should be really carefully using 'non-math' functions, like floor? Or should you perhaps 'never' use units, which is one of the reasons I like Mathcad so much.

In my case using round will solve my problem. Are there any more tips/tricks people should know of before using software like this? It scared the h*ll out of me when I found this out though :).
 
I was going to mention this earlier, but senior moment(s)... Since you are fretting about a quantity that is not particularly meaningful in the real world, you can set the zero threshold to something like 10, which means that Mathcad will ignore any remainders smaller than 10^-10 in its calculations, so that FLOOR(12.75m/1275mm) will result in 10. 10^-10 m is an angstrom, so that should be safe, although you could set it down to

This should done with care; otherwise, a global tolerance setting that high will give you silly results. Typically, advanced users will set the zero tolerance to 307, i.e., 10^-307 at the get-go.

TTFN
faq731-376
7ofakss
 
It's not true that Excel "gets around the binary floating point limitation". On the spreadheet it does some rounding of the final decimal place, that sometimes works to avoid the problem discussed here, and sometimes doesn't. On the whole I think it's more trouble than it's worth, especially since it can result in VBA and the spreadsheet giving different results.

More details in the links below, for anyone interested.



Doug Jenkins
Interactive Design Services
 
Yech... That sounds like a bunch of baloney, or the programmers were even more inept than described in the report. If the errors were accumulating as described, the initial range would have been wrong as well, so it should have been impossible to get the target anywhere near the range gate for the second detection. But, if they got the range correct on the first pulse, they were using delta time, and the second pulse range gate would be based on the delta time relative to the T0 of the second pulse.

A typical scenario is to start range counter at T0 for the first pulse. Detect SCUD at T1. Set range gates for second pulse at T2+(T1-T0)-deltaT*Vscud/c-gateT to T2+(T1-T0)-deltaT*Vscud/c+gateT, where deltaT=T2-T0. Regardless of whether T0 and T1 and T2 are only relative time or absolute time, since the calculations only involve their time difference, the offset error should be irrelevant.

I can't argue with the notion that the system time might have been off, but it should be moot anyway.


TTFN
faq731-376
7ofakss
 
Status
Not open for further replies.
Back
Top