int() function bug?

Discussions about CSEntry
Forum rules
New release: CSPro 8.0
Post Reply
provogrammer
Posts: 11
Joined: June 11th, 2018, 4:52 pm

int() function bug?

Post 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,
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: int() function bug?

Post 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;
provogrammer
Posts: 11
Joined: June 11th, 2018, 4:52 pm

Re: int() function bug?

Post by provogrammer »

Hi Josh,

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

Best regards,
Post Reply