Metric Conversion

E

El Camino

Howdy all

Im trying to do a fairly accurate metric conversion
between:

inches
feet
yards

Im pretty sure that i have the right number for conversion
between length type.

I have no problem switching between metric length(mm,cm,m)

When im trying to switch between imperial system, i run
into some accuracy problem. Feet and inches are fine, but
i have some problem with yards.

12 inches = 1 feet
1 feet = 12 inches
1 feet = 0.3333... yards
0.333... yards = 11.88 inches
0.333... yards = 0.999 feet

you get the point...

How can i keep the accuracy with out rounding
 
S

solex

El Camino,

Off the top of my head; In your functions that do the conversions you can
create a rounding rule that states if the number is within a specified unit
of a whole number then you can round up or round down. Try using the Mod
function for this and analyze the remainder.

Dan
 
A

Armin Zingler

El Camino said:
Howdy all

Im trying to do a fairly accurate metric conversion
between:

inches
feet
yards

Im pretty sure that i have the right number for conversion
between length type.

I have no problem switching between metric length(mm,cm,m)

When im trying to switch between imperial system, i run
into some accuracy problem. Feet and inches are fine, but
i have some problem with yards.

12 inches = 1 feet
1 feet = 12 inches
1 feet = 0.3333... yards
0.333... yards = 11.88 inches
0.333... yards = 0.999 feet

you get the point...

How can i keep the accuracy with out rounding


Accuracy will always be limited because any data type consumes a limited
amount of memory. Maybe using the Decimal data type can help you.
 
M

Michael Fitzpatrick

Use a scaler, or ratio method. That is set a numerator and a denominator and
to the math on the final value:

double dScale(double dVal, double dNumerator, double dDenominator)
{
return (dVal * dNumerator) / dDenominator;
}
In this way you'll always get the precision of the system with no
intermediate rounding errors.

This is a trick often used in Forth, and embedded systems where floating
point is expensive in CPU and code space and hardware.
 
C

Charlie Smith

El Camino,

The general rule here is to use a standard base and always convert
from it. If a number comes in inches, then keep it in inches and do
the conversion at the last step to the required units.

If you start with 7 inches then want feet, it would be 7/12. To go
to yards, start over with 7/36. To go to Meters use 7*0.0254. This
will minimize the decimal error.
 
C

Cor

Hi Armin,

Conversion of this kind of values will in general always stay a problem it
never will be posible to convert it 100% exactly and reconverting it again
although you can make the difference very small of course.

That has always been even before the computer did exist.

Just a thought

Cor
 
A

Armin Zingler

Cor said:
Conversion of this kind of values will in general always stay a
problem it never will be posible to convert it 100% exactly and
reconverting it again although you can make the difference very small
of course.

Hmmm...did I write something different? Or did you just want to add the
following sentence?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top