开发者

Metric and Imperial internal representation

I want to use metric and Imperial distance units in an app开发者_如何学Pythonlication. 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?


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜