Skip to content

Rounding Dimension Values from 3D InterOp

David Hornsby | 16-08-2017

Translating NIST (National Institute for Standards and Technology) standard test files with 3D InterOp

dimension-image.jpg
Hexagon pattern 1

 Recently I was asked a question about one of the standard test files generated by NIST (National Institute for Standards and Technology) that was being translated by our 3D InterOp translator.  The question concerned the diameter of a hole in the geometry with a dimension on it specifying ⌀0.238, which sounded innocuous enough at first.

dimension-image.jpg

On closer inspection the dimension in question was showing the size of a hole with a diameter of 0.2375 inches to 3 decimal places, which I found after a bit of searching is the ACME standard drill size for a hole to tap a 90% thread of size 0.3125” with 12 turns per inch.  It would appear to be a perfectly reasonable number but of course there is a gotcha, which is why I am writing this.

My test application printed out the nominal value of the dimension of the hole as 0.237500, the value I expected, however on examining the variable holding that value in the debugger I found it to be 0.237499999999999.  So my initial thought was that someone had done a floating point calculation somewhere and introduced an inaccuracy.  

One of the first things that a new developer in geometric modeling gets to learn is that calculations using floating point values can introduce tiny inaccuracies due to the precision of the floating point representation.  Many times I have heard developers discussing how code is behaving differently on two separate platforms because of a difference between two floating point values in the 14th place of decimals.  So I was half right in my guess about where this error came from, but this was not the result of a series of calculations.

In fact, due to the way floating point numbers are represented in computers 0.2375 does not have that exact value when assigned to a double in C++.  I can write some code to assign 0.2375 to a variable and when I look at it in the debugger it is represented as 0.237499999999999.  From the programming point of view it behaves nicely, in that multiplying by 10.0 yields 2.375 and dividing by 10 yields 0.02375.

Code snippet.jpg

In my test code I was simply using printf to output the nominal value, which looked fine because printf was printing out 6 decimal places giving 0.237500.  If we go back to the original question about a display of ⌀0.238 we can see that the dimension is set up to display with 3 decimal places of precision, and unfortunately that is exactly the precision that will get rounded down due to the inaccuracy of the floating point representation, any other precision would work as expected.

The way to generate the required output string is to treat the number we are trying to format in two parts, to the left and the right of the decimal point.  The left part is obtained by extracting the integer part of the floating point number, assuming the number is not too big to be held in an integer, which for a dimension is reasonable.  The right part is obtained by subtracting the integer part, then multiplying by 10.0 to the power of the number of decimal places, adding 0.5 then taking the integer part of the result.

Code snippet 2.jpg

The combination of having a number that is not represented exactly and a display precision such that it gets rounded down means that the chances of this situation occurring are pretty small.  I suspect this is not a coincidence, it is a good test case and I have to wonder if someone at NIST will be chuckling as they read this.

You might also like...

2 Min read
3D InterOp
Software developers in the manufacturing field are often tasked with implementing expert-level algorithms for 3D...
CNC Routing Software 1
7 Min read
3D InterOp
CNC routing software is an indispensable tool that gives manufacturers new levels of precision and speed in product...
Application Lifecycle Management Flow
4 Min read
CGM Modeler
When you hear the term, Application Lifecycle Management (ALM), you likely think about the process that a software...
8 Min read
CGM Modeler
What is Computer Aided Manufacturing The CAM Market Who Uses CAM Software? Trends in CAM What do CAM Software...
7 Min read
3D InterOp
Table of Contents Why industrial automation is important Advantages and Disadvantages of Industrial Automation The...
8 Min read
CGM Modeler
What do you do? What Exactly is FEM? What You Need to Know About Choosing a FEM Modeler FEM and Partial Differential...
5 Min read
CGM Modeler
Computational Fluid Dynamics (CFD) is a type of analysis that provides insight into solving complex problems, and...
2 Min read
CGM Modeler
WRL files are an extension of the Virtual Reality Modeling Language (VRML) format . VRML file types enable browser...
Voxel model example
3 Min read
CGM Modeler
Voxels are to 3D what pixels are to 2D. Firstly -- let’s examine what pixels actually are. Everything you see on your...
Point_cloud_torus
2 Min read
CGM Modeler
Point-cloud modeling is typically used in the process of 3D scanning objects. Rather than defining surfaces through...
Polygonal Modeling
2 Min read
CGM Modeler
Polygonal (or polyhedral) modeling is the most common type of modeling for video games and animation studios. This type...
BREP Model example
2 Min read
CGM Modeler
BRep modeling, or Boundary Representation modeling, is, in CAD applications, the most common type of modeling. BRep is...
4 Min read
3D InterOp
(Stratasys V650 Flex. Source: Javelin-Tech) 3D printing has emerged as a popular 3D model fabrication option. Be it...
CAD System Components
4 Min read
CGM Modeler
Effective computer-aided design (CAD) and computer-aided manufacturing (CAM) programs include the following main...
Voxeldance and Spatial
2 Min read
3D InterOp
To the uninitiated, 3D printing may seem a simple process — download your CAD file and hit print. But the world of...
29556
4 Min read
3D InterOp
Due to different workflows, it is not uncommon to find companies collaborating with one another (e.g. a...
dimension-image.jpg
2 Min read
3D InterOp
Recently I was asked a question about one of the standard test files generated by NIST (National Institute for...
4 Min read
3D InterOp
Part and parcel with model-based engineering is model translation. Because the model is now the specification, accurate...