If I do from __future__ import division then eval(1/2) gives me 0.5 as
expected. But if I do print input("enter a sum: ") and enter 1/2 as
the sum I get 0 as if I hadn't done the import. I thought input was
supposed to give the same behaviour as an eval on raw input -- why the
difference here? 12 2122
Try 1.0/2 instead of 1/2. Note that when I do eval("1/2") I get 0 not
0.5
-Denis
Le Sun, 11 Jan 2004 23:45:48 +0000, Tim Rowe
<tim@remove_if_ not_spam.digiti g.co.uk> a écrit : If I do from __future__ import division then eval(1/2) gives me 0.5 as expected. But if I do print input("enter a sum: ") and enter 1/2 as the sum I get 0 as if I hadn't done the import. I thought input was supposed to give the same behaviour as an eval on raw input -- why the difference here?
Denis Sarrazin wrote: Try 1.0/2 instead of 1/2. Note that when I do eval("1/2") I get 0 not 0.5
Please note: "from __future__ import division", see PEP 238 which
changes the behavior of the divisino operator.
My results with Python 2.3.3: from __future__ import division eval('1/2')
0.5 input()
1/2
0 eval(raw_input( ))
1/2
0.5 -Denis
Le Sun, 11 Jan 2004 23:45:48 +0000, Tim Rowe <tim@remove_if_ not_spam.digiti g.co.uk> a écrit :
If I do from __future__ import division then eval(1/2) gives me 0.5 as expected. But if I do print input("enter a sum: ") and enter 1/2 as the sum I get 0 as if I hadn't done the import. I thought input was supposed to give the same behaviour as an eval on raw input -- why the difference here?
Tim Rowe <tim@remove_if_ not_spam.digiti g.co.uk> writes: If I do from __future__ import division then eval(1/2) gives me 0.5 as expected. But if I do print input("enter a sum: ") and enter 1/2 as the sum I get 0 as if I hadn't done the import. I thought input was supposed to give the same behaviour as an eval on raw input -- why the difference here?
The input function is calling eval from the context of the module
where 'input' itself is defined. If you use "from __future__ import
division" in module A and have "print 3/2" in module B, the value of
3/2 in module B shouldn't be affected by the input, since module B
may depend on integer division having the old behavior.
The result is a little bit surprising at first glance though, so it
should probably be documented.
In article <7x************ @ruckus.brouhah a.com>, Paul Rubin <http@?.cx>
writes Tim Rowe <tim@remove_if_ not_spam.digiti g.co.uk> writes: If I do from __future__ import division then eval(1/2) gives me 0.5 as expected. But if I do print input("enter a sum: ") and enter 1/2 as the sum I get 0 as if I hadn't done the import. I thought input was supposed to give the same behaviour as an eval on raw input -- why the difference here?
The input function is calling eval from the context of the module where 'input' itself is defined. If you use "from __future__ import division" in module A and have "print 3/2" in module B, the value of 3/2 in module B shouldn't be affected by the input, since module B may depend on integer division having the old behavior.
The result is a little bit surprising at first glance though, so it should probably be documented.
I get this in pythonwin with 2.3.2
PythonWin 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)]
on win32.
Portions Copyright 1994-2001 Mark Hammond (mh******@skipp inet.com.au) -
see 'Help/About PythonWin' for further copyright information. from __future__ import division eval('1/2')
0.5
In python I get
Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright" , "credits" or "license" for more information. from __future__ import division eval('1/2')
0.5
so I guess pythonwin is broken in this respect.
--
Robin Becker
[Tim Rowe] If I do from __future__ import division then eval(1/2) gives me 0.5 as expected. But if I do print input("enter a sum: ") and enter 1/2 as the sum I get 0 as if I hadn't done the import. I thought input was supposed to give the same behaviour as an eval on raw input -- why the difference here?
[Paul Rubin[ The input function is calling eval from the context of the module where 'input' itself is defined. If you use "from __future__ import division" in module A and have "print 3/2" in module B, the value of 3/2 in module B shouldn't be affected by the input, since module B may depend on integer division having the old behavior.
Right!
So, the way to get eval() to respond to the import is to pass along
the current environment: from __future__ import division eval('9/2', globals())
4.5
Raymond Hettinger
Robin Becker <ro***@jessikat .fsnet.co.uk> writes: Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32 Type "help", "copyright" , "credits" or "license" for more information. from __future__ import division eval('1/2') 0.5 so I guess pythonwin is broken in this respect.
Huh? you get the expected result with both python and pythonwin. Neither
one is broken. The surprising result is from input(), not eval():
$ python
Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
Type "help", "copyright" , "credits" or "license" for more information. from __future__ import division print input('expressi on: ')
expression: 1/2
0 print eval('1/2')
0.5
That's because input evaluates its string in the context of a
different module which was compiled with old-style division. I don't
know of other functions that use eval like that, and input() should
be deprecated or eliminated anyway, so this isn't a big deal.
Thanks. I'd not realized that we could test out new changes to Python
within existing Python code. Cool.
-D
Le Mon, 12 Jan 2004 02:49:45 -0700, Jason Mobarak <jm**@unm.edu > a
écrit : Please note: "from __future__ import division", see PEP 238 which changes the behavior of the divisino operator. py****@rcn.com (Raymond Hettinger) writes: [Tim Rowe] If I do from __future__ import division then eval(1/2) gives me 0.5 as expected. But if I do print input("enter a sum: ") and enter 1/2 as the sum I get 0 as if I hadn't done the import. I thought input was supposed to give the same behaviour as an eval on raw input -- why the difference here? [Paul Rubin[ The input function is calling eval from the context of the module where 'input' itself is defined. If you use "from __future__ import division" in module A and have "print 3/2" in module B, the value of 3/2 in module B shouldn't be affected by the input, since module B may depend on integer division having the old behavior.
Right!
So, the way to get eval() to respond to the import is to pass along the current environment: from __future__ import division eval('9/2', globals())
4.5
Um, that's not how it works. Off the top of my head I'm not entirely
sure why eval() respects future statements and input() does not, but
it's probably easy enough to fix, if anyone cares (I don't think I
do).
Cheers,
mwh
--
Windows 2000: Smaller cow. Just as much crap. -- Jim's pedigree of
operating systems, asr
In article <7x************ @ruckus.brouhah a.com>, Paul Rubin <http@?.cx>
writes Robin Becker <ro***@jessikat .fsnet.co.uk> writes: Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32 Type "help", "copyright" , "credits" or "license" for more information. >>> from __future__ import division >>> eval('1/2') 0.5 >>>
I guess I'm 'broken' :)
--
Robin Becker This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Michael Foord |
last post by:
Here's a little oddity with 'print' being a reserved word...
>>> class thing:
pass
>>> something = thing()
>>> something.print = 3
SyntaxError: invalid syntax
>>> print something.__dict__
{}
|
by: joel |
last post by:
I have a table which I want to update by dividing one field into
another. The update runs with no errors, but the results come out as
only a positive integer number.
The datatype for the result field is float.
The datatype for the other fields are int.
I have tried testing by dividing 7 by 10000, which should give me
0.0007. The result in the database is 0.
|
by: Marcin |
last post by:
How I can make division of two numbers placed in arrays, example:
short int a = {2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2};
short int b = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2};
short int result = a / b; 'division
without additional lib's and header files, only in standart C.
|
by: seb.haase |
last post by:
Hi,
Is it true that that "Python 3000" is dead ?
Honestly I think that e.g. changing 5/2 to be 2.5 (instead of 2) would
just break to much code :-(
On the otherhand I'm using Python as "Matlab replacement" and would
generally like 5/2 ==2.5
So, I was contemplating to default all my modules/scripts to start with
"from __future__ import division"
but if it is never coming (in this decade, that is) then it would be a
|
by: jmdocherty |
last post by:
All,
I've been trying to set up a CSS layout and all seems well in Firefox
but in Safari it just seems to be plain weird! I hope someone can help
tell me whether this is a problem with my code or a Safari oddity
(which if it is, any top-tips to fix it would be most welcome!).
Objective:
I want to have the meat of the viewport divided into 3 columns (a fixed
margin (say 140px) and 2 scalable panes on the right each of which
| |
by: michael.f.ellis |
last post by:
The following script puzzles me. It creates two nested lists that
compare identically. After identical element assignments, the lists
are different. In one case, a single element is replaced. In the
other, an entire column is replaced.
---------------------------------------------------------------------------------------
'''
An oddity in the behavior of lists of lists. Occurs under
Python 2.4.3 (#69, Mar 29 2006, 17:35:34)
|
by: Mike S |
last post by:
Does anyone know the logic behind why in VB.NET the result of a
floating-point division ('/') is -rounded- on being converted to an
integer type, such as with statements like
Dim x As Integer = 2/3 'after assignment, x is 1, whereas a sane person
would say it should be 0
Does Microsoft have a reason for this design decision? I understand
that this type of rounding can reduce the overall error in long
computation chains by reducing the...
|
by: kermit |
last post by:
For a long time,, There has been a discussion of trueFor division
versus integer division in Python.
I myslef prefer that / be used for integer division since almost
always, I want the result of the
division be truncated to integer.
However, today I reviewed the method to be used in Python to get true
division, and this gave
|
by: jamesonang |
last post by:
Supposed unsigned int(32 bits) is the largest number that computer can
represent with a single variable.
Now, i have a big integer ( less than 64 bit, but great than 32 bit) .
i represent it by this way:
unsigned int dividend :
divident store low 32 bits of big integer, dividend store high 32
bits of big integer.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |