Link to home
Start Free TrialLog in
Avatar of jturpin
jturpinFlag for United States of America

asked on

Problem with int() and trunc() !

Math problem !!!!!!!!

normally int(226.0) returns 226 and
trunc(226.0) returns 226

But I am getting both returning 225
in the code below !


   function EvenCmClasses(OriginalHt:Double) : Double;
   {Converts X to an even cm class, and rounds to two       decimals}
   var
      Ht100, Ht1000, InitialClass : Double;
      CorrectIt : LongInt;
   begin
      Ht100:= OriginalHt*100.0;
      Ht1000:= OriginalHt*1000.0;
      CorrectIt:= Trunc(Ht100);
      InitialClass:= CorrectIt/100.0;
     {InitialClass:= Int(Ht100)/100.0;}

The OriginalHt comes from a sum of a
TOvcPictureArrayEditor set to datatype = ptfDouble

Watches shows OriginalHt = 226

Can anyone give me some help with this ?

John.
Avatar of kjteng
kjteng

i run your code and find the OriginalHt=226 and initialClass=226.
How did you get 225?
what is ptfDouble? a pointer type?
I agree with kjteng.

Are you trying to round to 2 decimal places?  If so, then your logic is currently "(Int(Num * 100)) / 100".  However, this will always round down, e.g. 226.236 becomes 226.23.  To fix this, add 0.5 after the Int part, i.e. "(Int((Num * 100) + 0.5)) / 100".

Then 226.234 -> 226.23, but 226.235 -> 226.24

In your particular example, I get 226 like kjteng.

Regards,
JB
Avatar of jturpin

ASKER

I have no idea why it is returning 225. I
was hoping someone could give me some
pointers.

ptfDouble is one of the enumerated values
used by Orpheus. It defines the field data
type as double.

Did you both use orpheus ?

In this routine I need to cut the number off
after the second decimal, without rounding.

John.
Hi again,

I don't use Orpheus, although that shouldn't matter.

This is a very strange problem you have.  If you like, you can e-mail me a sample app which gives the incorrect value, and I can have a look at it.  My e-mail address is davekw@iafrica.com.

Regards,
JB
The problem MAT BE due to the actual parameter passed to the function. Check the variable type and also watch the value of the number before it is passed to the function. I suspect that the value passed to the function was 225.99999999 and the .999999 was trucated.

I do not use orpheus though i heard of that before.
The problem MAY BE due to the actual parameter passed to the function. Check the variable type and also watch the value of the number before it is passed to the function. I suspect that the value passed to the function was 225.99999999 and the .999999 was trucated.

I do not use orpheus though i heard of that before.
ASKER CERTIFIED SOLUTION
Avatar of rickpet
rickpet

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial