Page 1 of 1

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

Posted: Mon Jan 19, 2015 12:15 pm
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

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

Posted: Mon Jan 19, 2015 12:32 pm
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.

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

Posted: Mon Jan 19, 2015 12:41 pm
by manumart1
Thanks,
I did not notice about Math.IntPower