Metric and Imperial internal representation

北城余情 提交于 2019-12-11 03:58:03

问题


I want to use metric and Imperial distance units in an application. One way of doing this is tagging a number with the unit. I don't want to go this route.

I want to use one internal representation that can exactly hold imperial down to 1/64 of an inch and 0.2mm. I have come up with 8128 RSU (really small units) that is divisible by 64 and 254, which does what I want.

This is close to 2^13, and it has got me wondering if there is a more optimal way of doing this?

Any ideas?


回答1:


A slightly simpler solution might be to use "Other small units" so that 1 OSU = 1/320 mm. With this convention, 1/64" would be 127 OSU and 0.2mm would be 64 OSU. Since 127 and 64 are relative primes, this is the lowest denominator you can get.




回答2:


Ran across this Q&A searching for something else (how to go from a float to a metric unit prefix) and thought I'd share one implementation I've seen.

Mentor Graphics' PADS Layout is a printed-circuit board (PCB) layout program that can deal in three scales: inches, millimeters, and mils (1 mil = 0.001 inch), the last of which is one of the US industry standard measurements. This is a business where imprecision from floating point rounding error leads to defective or unusable products.

To represent coordinates internally, they use what they call "basic" units which are exactly 2/3 nm. So there are 1.5e6 basic units to the millimeter and 38100 to the mil. This lets them represent distances of 0.01 thousandths of an inch or 10 nanometers as an integral number of these "basic" units (381 and 15, respectively). Using 32-bit integers for coordinates, the workspace is just over 112 inches (2.86 m) on each axis.




回答3:


To avoid rounding errors and programming confusion, my advise will be to use internally one unit. So you can do the conversion on the outside:

  • gui
  • import/export
  • possibly database, but better leave that to the internal unit unless you are forced to work with that database.

In the past, several projects have failed because of unit confusion.



来源:https://stackoverflow.com/questions/4312701/metric-and-imperial-internal-representation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!