Operation: Division

Post your questions and help other users.

Moderator: Martin

Post Reply
xxx
Posts: 39
Joined: 18 Mar 2013 18:51

Operation: Division

Post by xxx » 01 Oct 2014 13:04

Hello,

If I try to execute the following operation in a script, I get not the expected result.

x=5/2;

The result is 2 instead of 2.5.

May be there is a simple reason for and it is easy to solve.

sambarlick
Posts: 96
Joined: 27 Jul 2014 10:40

Re: Operation: Division

Post by sambarlick » 02 Oct 2014 09:12

Instead of 5/2 try 5/2.0
worked for me.

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Operation: Division

Post by Martin » 02 Oct 2014 11:41

Hi,

Automagic uses integer math when both operands are integers. You can avoid it by using a floating point number for the divisor or dividend:
x = 5/2.0;

Regards,
Martin

Edit: Question was asked twice, merged the two topics.

xxx
Posts: 39
Joined: 18 Mar 2013 18:51

Re: Operation: Division

Post by xxx » 02 Oct 2014 12:49

Strange, but it works. :))
Thank you so much.

User avatar
MURTUMA
Posts: 697
Joined: 05 Mar 2013 22:43

Re: Operation: Division

Post by MURTUMA » 02 Oct 2014 15:59

@Martin: On a slightly related question.. Should the 2.5 round up to 3 instead of 2? Or are the integer calculations only cropping decimals off?

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Operation: Division

Post by Martin » 03 Oct 2014 09:06

Integer division is defined to round towards zero, the same is used in Java as well: 5/2-->2, -5/2-->-2

User avatar
Bushmills
Posts: 286
Joined: 23 Sep 2014 21:56

Re: Operation: Division

Post by Bushmills » 03 Oct 2014 14:19

For rounding the result of an operation on integers, multiply one of the operation factors by 2. Add 1 to the result, then divide by 2.

For your example 5/2, this means, (2*5/2+1)/2.

This little trick is useful when you want to stick to integer operands, and not resort to using floats for a simple operation like rounding.

Post Reply