On 18 mai, 20:02, "Somebody" <someb...@cox.netwrote:
So, what to do? Cast x to a double.
Change the line in the program to:
double z = static_cast<double>( x ) / y;
and the output is
0.333333
I know that :)... I was only including psuedo-code... but the point is:
double x = 100;
double cx = 300;
double ratio = x / cx; // = 0.33333
double newwidth = 300;
double newpos = ratio * newwidth; // result = 99, NOT 100 which is what it
should be.
Are you kidding? The result won't be 100.0, but it will
be fairly close to 100. (In fact, it might actually end up as
100.0; it does on my machine, anyway. But of course, you can't
count on it; you can only count on it being fairly close.)
My solution of storing the numerator and denomenator to get a
real scaling factor worked.
It's guaranteed to be 100% accurate. And very slow, and liable
to overflow in integral arithmetic. Depending on the
application, it might be what's needed, or it might not be.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34