Page 1 of 1

int() function bug?

Posted: April 9th, 2019, 6:08 am
by provogrammer
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,

Re: int() function bug?

Posted: April 9th, 2019, 6:24 am
by josh
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:
function truncate(x)
    if x >= 0 then
        truncate = int(x);
    else
        truncate = -int(abs(x));
    endif;
end;

Re: int() function bug?

Posted: April 9th, 2019, 7:12 am
by provogrammer
Hi Josh,

Thanks for reply, I will use your solution for now,

Best regards,