Solved

Problem with int() and trunc() !

Posted on 1998-05-20
7
516 Views
Last Modified: 2010-04-06
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.
0
Comment
Question by:jturpin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 2

Expert Comment

by:kjteng
ID: 1346724
i run your code and find the OriginalHt=226 and initialClass=226.
How did you get 225?
what is ptfDouble? a pointer type?
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1346725
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
0
 
LVL 1

Author Comment

by:jturpin
ID: 1346726
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:JimBob091197
ID: 1346727
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
0
 
LVL 2

Expert Comment

by:kjteng
ID: 1346728
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.
0
 
LVL 2

Expert Comment

by:kjteng
ID: 1346729
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.
0
 
LVL 3

Accepted Solution

by:
rickpet earned 200 total points
ID: 1346730
As stated earlier by kjteng...you are most likely passing 225.999999999, Delphi's IDE show's this as 226 and not 225.999999999, even though it stores it and handles it as 225.999999999. Step through the code and watch the variables...you'll see what I mean.

Rick

function MyTrunc(value: Double): double;
begin
  value := value * 100.0;
  value := trunc(value);
  result := value/100.0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  myDouble: double;
begin
  myDouble := 225.999999999;
  Label1.Caption := FloatToStr(MyTrunc(myDouble));
end;

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question