Hi,
I have problem with int() function in CSPro, especially with negatif number,
errmsg("%f int: %f",-7.7543,int(-7.7543));
return are :
-7.7543 int: -8.0000
I think it should return -7.0000 not -8.0000
Thanks,
int() function bug?
Re: int() function bug?
In some other programming languages int() simply truncates the decimal portion. In CSPro int() gives you the smallest integer less than the argument. So in the case of negative numbers int(-1.23) will return -2, not -1.
To get the behavior you want you could first take the absolute value using the abs() function, then use int() to truncate and then negate the result. You could wrap into your function like this:
To get the behavior you want you could first take the absolute value using the abs() function, then use int() to truncate and then negate the result. You could wrap into your function like this:
function truncate(x)
if x >= 0 then
truncate = int(x);
else
truncate = -int(abs(x));
endif;
end;
if x >= 0 then
truncate = int(x);
else
truncate = -int(abs(x));
endif;
end;
-
- Posts: 11
- Joined: June 11th, 2018, 4:52 pm
Re: int() function bug?
Hi Josh,
Thanks for reply, I will use your solution for now,
Best regards,
Thanks for reply, I will use your solution for now,
Best regards,