arachide
arachide

Reputation: 8066

get the abs value of long long integer

I try to use the codes below to get the abs value of a long long type integer;

long long v=abs(originalValue);

It works as I expected until the value of v exceeds 1073741824 (1G)

If v is 2147482648, abs(v) is -2147482648.

If v is 10737418240, abs(v) is -2147482648 also.

I do not understand what causes these happened.

Welcome any comment

Thanks

interdev

Upvotes: 1

Views: 629

Answers (2)

nos
nos

Reputation: 229058

abs takes an int as an argument, for a long long use llabs

Upvotes: 1

BoltClock
BoltClock

Reputation: 723498

Use llabs() instead:

long long v = llabs(originalValue);

Upvotes: 3

Related Questions