Keith Thompson wrote:
CBFalconer <cb********@yah oo.comwrites:
>ga*****@hotmail .com wrote:
>>Konstantin Miller wrote:
ga*****@hotmail .com wrote:
I need to do just a few multiplies of long integers and have a
divide by ten. Can I avoid using floats? Can I use two longs
as a 64 bit value somehow? Thanks.
Can you specify more clearly what you want to do? :-)
I have some long values in Liters and I want to convert to gallons
(and back). If I could do integer multiplication and divide by 10
I could avoid the float library. Thanks.
[1] c:\stds>units gallon litre
* 3.7854118
/ 0.26417205
As you can plainly see, the conversion factors are nowhere near
integral. Besides which, no float library should be required to do
the multiplication or division. The final output is another
matter.
The exact ratio, in lowest terms, is 473176473 / 125000000 (or,
expressed as a real number, exactly 3.785411784). (1 liter is 1000
cubic centimeters; 1 US gallon is 231 cubic inches; 1 inch is 2.54
centimeters.)
This is pure coincidence, but a few hours ago I published (here) my
ratapprx program for rational approximations. Applied to this
problem it gives:
[1] c:\c\ratapprx>r atapprx 3.7854118 10000
Usage: ratvalue [number [maxnumerator]]
number defaults to PI, maxnumerator to 500
Rational approximation to 3.7854118000000 00
2 / 1 = 2.0000000000000 00 with error 1.7854118000000 00
3 / 1 = 3.0000000000000 00 with error 0.7854118000000 00
4 / 1 = 4.0000000000000 00 with error 0.2145882000000 00
11 / 3 = 3.6666666666666 67 with error 0.1187451333333 33
15 / 4 = 3.7500000000000 00 with error 0.0354118000000 00
19 / 5 = 3.8000000000000 00 with error 0.0145882000000 00
34 / 9 = 3.7777777777777 78 with error 0.0076340222222 22
53 / 14 = 3.7857142857142 86 with error 0.0003024857142 86
458 / 121 = 3.7851239669421 49 with error 0.0002878330578 51
511 / 135 = 3.7851851851851 85 with error 0.0002266148148 15
564 / 149 = 3.7852348993288 59 with error 0.0001769006711 41
617 / 163 = 3.7852760736196 32 with error 0.0001357263803 68
670 / 177 = 3.7853107344632 77 with error 0.0001010655367 23
723 / 191 = 3.7853403141361 26 with error 0.0000714858638 74
776 / 205 = 3.7853658536585 36 with error 0.0000459463414 63
829 / 219 = 3.7853881278538 81 with error 0.0000236721461 19
882 / 233 = 3.7854077253218 89 with error 0.0000040746781 11
2699 / 713 = 3.7854137447405 33 with error 0.0000019447405 33
3581 / 946 = 3.7854122621564 48 with error 0.0000004621564 48
4463 / 1179 = 3.7854113655640 37 with error 0.0000004344359 63
8044 / 2125 = 3.7854117647058 82 with error 0.0000000352941 18
So I guess it has definite uses in allowing elimination of
overflows in calculations such as the OPs. I doubt if he needs
anything better than 882 / 233 (or 233 / 882 for the inverse
conversion). If he just installs the magic numbers without comment
he deserves anything that happens to him.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>