Comments on: Ada to C++ conversion of floating types filter
http://ask.metafilter.com/77179/Ada-to-C-conversion-of-floating-types-filter/
Comments on Ask MetaFilter post Ada to C++ conversion of floating types filterMon, 26 Nov 2007 19:09:34 -0800Mon, 26 Nov 2007 19:09:34 -0800en-ushttp://blogs.law.harvard.edu/tech/rss60Question: Ada to C++ conversion of floating types filter
http://ask.metafilter.com/77179/Ada-to-C-conversion-of-floating-types-filter
I am currently working on a by the book Ada to C++ conversion of legacy code and am having a bit of trouble coming with a C++ solution for a particular Ada idiom. <br /><br /> The Ada construct specifies floats with various levels of precision and then uses these types in bit packed fields:<br>
<br>
Delta15: constant := (1.0 / 2**15)<br>
type Data16 is delta Delta15 range -1.0 .. (1.0 - Delta15);<br>
for Data16 'Size use 16;<br>
<br>
...<br>
<br>
type X is <br>
record<br>
Y: Data16;<br>
Y2: Data16;<br>
end record;<br>
<br>
My question is this, how do I represent this in C++? Double's and floats are 64 bits by default. Essentially I need to create a 16 bit fixed point double. Any ideas are welcome. Thanks for your help.post:ask.metafilter.com,2007:site.77179Mon, 26 Nov 2007 19:05:06 -0800caflores22Programmingc++adaBy: aubilenon
http://ask.metafilter.com/77179/Ada-to-C-conversion-of-floating-types-filter#1146380
The language has no native support for this. <br>
<br>
But you can create a fixed_point16 class (or FixedPoint16 if you prefer) and make it function just like a built-in.<br>
<br>
btw, "float" is 32 bit, "double" is 64.comment:ask.metafilter.com,2007:site.77179-1146380Mon, 26 Nov 2007 19:09:34 -0800aubilenonBy: AndrewStephens
http://ask.metafilter.com/77179/Ada-to-C-conversion-of-floating-types-filter#1146395
I imagine that since the original project was written in ADA, the precision of the numbers is very important. <br>
Floating point in C(++) just compiles down to the floating point implementation of whatever processor you are compiling for. If you want the exact ADA behavior you will need to create your own class and override the mathematical operators to work the same way as the ADA runtime. <br>
This seems easy, but I imagine that the devil is in the details of reimplementing the exact semantics of the ADA type system.comment:ask.metafilter.com,2007:site.77179-1146395Mon, 26 Nov 2007 19:22:07 -0800AndrewStephensBy: b1tr0t
http://ask.metafilter.com/77179/Ada-to-C-conversion-of-floating-types-filter#1146404
Get yourself a copy of <a href="http://www.amazon.com/exec/obidos/ASIN/0201700735/metafilter-20/ref=nosim/">Stroustrup</a>, and best luck!comment:ask.metafilter.com,2007:site.77179-1146404Mon, 26 Nov 2007 19:30:56 -0800b1tr0tBy: jepler
http://ask.metafilter.com/77179/Ada-to-C-conversion-of-floating-types-filter#1146466
This kind of number is called "fixed-point", as opposed to "floating-point". In C and C++, fixed-point arithmetic must be explicitly coded as functions or classes, it's not built into the language. <br>
<br>
There are many web pages and presumably books on floating-point math. One I found first was <a href="http://www.ddj.com/cpp/184401992">dr dobb's</a>.<br>
<br>
The basics are simple: addition and subtraction are the same as for integers (unless a special provision for overflow is required, such as saturation). multiplication is integer multiplication to a wider integer type followed by a shift by the number of fractional bits. division is a shift followed by an integer division. Like addition and subtraction, multiplication and division can also overflow the representable values (and there's division by zero too).<br>
<br>
In C++ it would seem natural to make both the number of fractional places and the underlying integral type template parameters. I don't know if C++ has a facility for specifying "the integral type with at least M more bits than this type" or "the integral type with at least twice as many bits as this type", both of which are things you might want when working with fixed-point maths.comment:ask.metafilter.com,2007:site.77179-1146466Mon, 26 Nov 2007 20:19:55 -0800jeplerBy: polyglot
http://ask.metafilter.com/77179/Ada-to-C-conversion-of-floating-types-filter#1146702
As jepler noted, that is a fixed-point number, not a float, i.e. it contains only mantissa and no exponent. The reason they did this is almost certainly performance; when the program was written, floating point units were likely horridly slow whereas now they are not. If performance was the only reason and the application is to run on modern PC-class hardware, feel free to use a float since that will have 23 bits of mantissa whereas the original implementation had 16.<br>
<br>
If they did fixed-point like that for repeatability reasons, you want to continue using fixed point.<br>
<br>
In most C++ compilers, a "short" will be 16 bits, so you want to use that. A signed short gives you [-32768, 32767], which you will note is exactly 32768 times larger than [-1.0, 1.0-2<sup>-15</sup>], therefore your scale factor between the fixed-point format and float is 32768; you'll need that for initialisation and I/O purposes.<br>
<br>
Addition and subtraction are just integer arithmetic, you don't have to do anything special except maybe check for overflows. Multiplication is integer multiplication into a 32-bit value (16 bits * 16 bits gives 32 bits) then right-shift by 16 bits to arrive at the correct scaling.<br>
<br>
You might want to wrap it in a class and provide all the operators including conversions and arithmetic (inline!), at least that's what I'd do. Google <a href="http://www.google.com.au/search?hl=en&q=fixed+point+arithmetic+techniques&btnG=Google+Search&meta=">has more to say</a>.comment:ask.metafilter.com,2007:site.77179-1146702Tue, 27 Nov 2007 02:41:35 -0800polyglotBy: plinth
http://ask.metafilter.com/77179/Ada-to-C-conversion-of-floating-types-filter#1146751
There's two reasonable approaches to this problem: a semantic approach and a pragmatic approach.<br>
<br>
The semantic approach would be to assume that using 16 bit fixed point arithmetic is important both due to performance and size constraints and that to be true to ADA, you would define a Fixed16 class which has all the appropriate arithmetic operators. This is reasonable and an interesting exercise if you like figuring out fixed point math.<br>
<br>
The pragmatic approach is to define a numeric class and use that instead. You can make the actual representation be 16 bit fixed if you like, but do most intermediate arithmetic in float or double units converting back and forth. This will add an expense, but has the added benefit of later optimization by switching to the approach above. The reason to go this way is that it gets you going faster with fewer bugs at the cost of performance.comment:ask.metafilter.com,2007:site.77179-1146751Tue, 27 Nov 2007 05:57:20 -0800plinthBy: goingonit
http://ask.metafilter.com/77179/Ada-to-C-conversion-of-floating-types-filter#1148196
Side-note: beware of assuming that "short", "int", "long" etc. have particular lengths! Instead use stdint.h and its types int16_t, uint16_t, int32_t...<br>
<br>
Especially when working with older hardware and situations (such as these) when storage size and bit position actually matters, this is a must.<br>
<br>
(To solve your problem, you're best off reserving 16 bits of space and using your own functions to solve the problem)comment:ask.metafilter.com,2007:site.77179-1148196Wed, 28 Nov 2007 07:36:05 -0800goingonit