ENTIER(Math.Power(5, 3)) = 124

Programming language questions
Post Reply
manumart1
Posts: 67
Joined: Tue Sep 17, 2013 6:25 am

ENTIER(Math.Power(5, 3)) = 124

Post by manumart1 »

I find somehow disturbing that ENTIER(Math.Power(5, 3)) = 124
because j = 125 if
x := Math.Power(5, 3);
j := ENTIER(x);

Code: Select all

PROCEDURE PowerEntier*;
	VAR x, y: REAL; xe, ye, k: LONGINT;
BEGIN
	x := Math.Power(5, 3); y := 5.0 * 5.0 * 5.0;
	xe := ENTIER(x); ye := ENTIER(y);
	k := ENTIER(Math.Power(5, 3)); (* !! *)
	StdLog.String("x = "); StdLog.Real(x); StdLog.Ln;
	StdLog.String("y = "); StdLog.Real(y); StdLog.Ln;
	StdLog.String("xe = "); StdLog.Int(xe); StdLog.Ln;
	StdLog.String("ye = "); StdLog.Int(ye); StdLog.Ln;
	StdLog.String("k = "); StdLog.Int(k); StdLog.Ln; (* !! *)
	StdLog.Bool(Math.Power(5, 3) = 5.0 * 5.0 * 5.0); StdLog.Ln; (* !! *)
	StdLog.Real(Math.Power(5, 3) - 5.0 * 5.0 * 5.0); StdLog.Ln; (* !! *)
	StdLog.Ln
END PowerEntier;

x =  125.0
y =  125.0
xe =  125
ye =  125
k =  124
 $FALSE
 -1.387778780781446E-17
cfbsoftware
Posts: 55
Joined: Wed Sep 18, 2013 10:06 pm
Contact:

Re: ENTIER(Math.Power(5, 3)) = 124

Post by cfbsoftware »

1. Read the discussion Numerical Analysis (including the references regarding rounding) contained in some of those posts.

2. For this particular example use Math.IntPower to get the answer you want.
manumart1
Posts: 67
Joined: Tue Sep 17, 2013 6:25 am

Re: ENTIER(Math.Power(5, 3)) = 124

Post by manumart1 »

Thanks,
I did not notice about Math.IntPower
Post Reply