Floor value in math operators

A

Avi

Hi,

This will be a very simple question to ask all the awesome programmers
here:

How can I get answer in in decimals for such a math operator:

3/2

I get 1. I want to get 1.5

Thanks in advance,
Avi
 
C

Chris Rebert

Hi,

This will be a very simple question to ask all the awesome programmers
here:

How can I get answer in in decimals for such a math operator:

3/2

I get 1. I want to get 1.5

Add the following line to the top of your program (or use Python 3.0+):
from __future__ import division

To get integer division when this is enabled, use the integer division
operator, //
e.g. x = 3//2 #x=1

Cheers,
Chris
 
D

Diez B. Roggisch

Avi said:
Hi,

This will be a very simple question to ask all the awesome programmers
here:

How can I get answer in in decimals for such a math operator:

3/2

I get 1. I want to get 1.5

You don't say which python-version you have. Depending on that, the answer
is different.

In python2.5 and before, dividing two integers will always return an
integer.

So for your problem, you need to do

3.0 / 2.0

At least one of the involved numbers must be a float.

However, from python2.5 the division operator behavior changed if desired -
if you do
1


you get what you expect. The old division operator is still available, as //

In python2.6 and 3.x, the new behavior is standard.

Diez
 
D

Diez B. Roggisch

In python2.6 and 3.x, the new behavior is standard.

Apparently that is nonsense - it seems to be not standard for 2.6. Which
Makes sense I guess.

Diez
 
D

David Smith

Avi said:
Hi,

This will be a very simple question to ask all the awesome programmers
here:

How can I get answer in in decimals for such a math operator:

3/2

I get 1. I want to get 1.5

Thanks in advance,
Avi

I'm going to assume your operands are variables instead of numeric
literals. Why not consider doing a type conversion to float or Decimal
and then perform the division?

--David
 
A

AggieDan04

I'm going to assume your operands are variables instead of numeric
literals.  Why not consider doing a type conversion to float or Decimal
and then perform the division?

Because float(x) and Decimal(x) fail for complex numbers and lose
precision if x is a rational or a multi-precision float.
 
J

John Machin

Because float(x) and Decimal(x) fail for complex numbers and lose
precision if x is a rational or a multi-precision float.

What is the likelihood that the OP would get 3 / 2 -> 1 if he was
working with rationals or multi-precision floats?

What is the likelihood that the OP would ask such a question if he was
working with rationals or multi-precision floats?
 
D

David Smith

AggieDan04 said:
Because float(x) and Decimal(x) fail for complex numbers and lose
precision if x is a rational or a multi-precision float.

The OP didn't ask for anything complicated or high precision -- just
wanted to divide two integer values and get a float/Decimal output.

--David
 
A

Avi

The OP didn't ask for anything complicated or high precision -- just
wanted to divide two integer values and get a float/Decimal output.

--David

Wow! I am overwhelmed by how much support I have at my finger tips. I
was hesitant to ask such a silly question and was worrying of being
ridiculed.

Clearly, I have gained a lot by reading everyone's responses.

Thanks!
Avi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top