473,811 Members | 3,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange behaviour of floating point constants in imported modules

Hi,

We are distributing our Python application as the short main script (.py
file) and a set of modules compiled to the .pyc files. So far, we have
always treated .pyc files as portable between platforms, but recently we
have discovered an annoying problem. In a module, there is the following
code fragment:

Deg2Rad = math.pi/180.0
angleEPS = 0.5
angle0B = angleEPS*Deg2Ra d

which calculates 'angle0B' as the angle of a half of a degree, converted
to radians. The module has been compiled on an English Windows XP
machine, and then tested on a Polish Windows XP workstation.

What was our astonishment, when various exceptions started to be raised
on a test machine (no problem on the original English-version Windows
XP). We have traced them to the fact, that both angleEPS and angle0B
were found to be ZERO (!!!), whereas in reality, angle0B is about 0.008.
And this all happened silently, without any error during the import of
the module!

What's the reason of this error? I start thinking, that it may be
related to the fact, that the decimal point on the Enlish Windows XP is
the '.' character, and on the Polish one - ','.

Is there a good method to avoid this kind of problems? How to make such
distributed modules really portable?

Thanks in advance
--
Tomasz Lisowski
Jul 19 '05 #1
4 1467
On Mon, 23 May 2005 12:39:00 +0200, Tomasz Lisowski
<to****@notmyis p.pl> wrote:
Hi,

We are distributing our Python application as the short main script (.py
file) and a set of modules compiled to the .pyc files. So far, we have
always treated .pyc files as portable between platforms,
There is no guarantee at all that a .pyc file is good for any purpose
outside the machine that produced it. In practice, however, you
*should* be able to rely on no surprises if you have the same platform
and the same version of Python; do you?

How did you transfer the .pyc files from one box to the other? A
Windows installer? A ZIP file? FTP using text mode? Plain-text
attachments to an e-mail message?

but recently we
have discovered an annoying problem. In a module, there is the following
code fragment:

Deg2Rad = math.pi/180.0
angleEPS = 0.5
angle0B = angleEPS*Deg2Ra d

which calculates 'angle0B' as the angle of a half of a degree, converted
to radians. The module has been compiled on an English Windows XP
machine, and then tested on a Polish Windows XP workstation.

What was our astonishment, when various exceptions started to be raised
on a test machine (no problem on the original English-version Windows
XP). We have traced them to the fact, that both angleEPS and angle0B
were found to be ZERO (!!!), whereas in reality, angle0B is about 0.008.
What evidence do you have? Have you disassembled the .pyc file on both
boxes and diff'ed the results? Have you computed checksums on both
boxes?
And this all happened silently, without any error during the import of
the module!

What's the reason of this error? I start thinking, that it may be
related to the fact, that the decimal point on the Enlish Windows XP is
the '.' character, and on the Polish one - ','.
This is *extremely* unlikely. Firstly, you are (I understand) talking
about a .pyc file, that was produced on an English Windows box. Even
though the "180.0" and the "0.5" are visible as character strings in
the .pyc file, Python sure doesn't use the locale when it loads a .pyc
file.

Secondly, even if you are talking about a .py file, Python takes
absolutely no notice of the locale when it compiles the .py file.
Polish programmers write "0.5", not "0,5". Read the language reference
manual, section 2.4.5 -- it uses ".", not "whatever the decimal point
character might be in your locale". If it did depend on locale, you
would need a locale declaration at the top of the file, if one wanted
..py files to be portable internationally ; ever seen or heard of such a
declaration?

Thirdly, if the dot was interpreted as something other than a decimal
point, then what? Perhaps assign a tuple (0, 5), or perhaps a syntax
error; zero is achieved under what conditions?

It's more likely that the .pyc file has been damaged somehow. AFAIK
they don't have checksums.

Is there a good method to avoid this kind of problems? How to make such
distributed modules really portable?


Distribute source.

HTH,

John

Jul 19 '05 #2
This may be relevant to the problems you're seeing:
https://sourceforge.net/tracker/?fun...&group_id=5470

The short story, as the tracker item paints it, is that setting
LC_NUMERIC to anything other than 'C' can give results like the ones you
describe---Python itself should never do this, but third parties code
may.

A web search for python LC_NUMERIC should turn up more about this topic,
probably even some past threads on this mailing list/newsgroup.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFCkctNJd0 1MZaTXX0RAmBsAJ 9P92+JRG4n/wZhewWB6+lXzy7n dQCdEbka
hlFs6R9FAAuG8i+ mey3Sw0o=
=smLI
-----END PGP SIGNATURE-----

Jul 19 '05 #3
> There is no guarantee at all that a .pyc file is good for any purpose
outside the machine that produced it. In practice, however, you
*should* be able to rely on no surprises if you have the same platform
and the same version of Python; do you?
Python 2.3.5 and 2.3.3 on the test machine - it is the same major and
minor version of the interpreter. I think it should be fine. The
platform - Windows XP on both machines - difference is in the language
version of Windows, and in the locale setting of the decimal point ('.'
and ',')

How did you transfer the .pyc files from one box to the other? A
Windows installer? A ZIP file? FTP using text mode? Plain-text
attachments to an e-mail message?
E-mailed in a ZIP file
but recently we
have discovered an annoying problem. In a module, there is the following
code fragment:

Deg2Rad = math.pi/180.0
angleEPS = 0.5
angle0B = angleEPS*Deg2Ra d

which calculates 'angle0B' as the angle of a half of a degree, converted
to radians. The module has been compiled on an English Windows XP
machine, and then tested on a Polish Windows XP workstation.

What was our astonishment, when various exceptions started to be raised
on a test machine (no problem on the original English-version Windows
XP). We have traced them to the fact, that both angleEPS and angle0B
were found to be ZERO (!!!), whereas in reality, angle0B is about 0.008.

What evidence do you have? Have you disassembled the .pyc file on both
boxes and diff'ed the results? Have you computed checksums on both
boxes?


I have prepared a 'debug' version of the module, printing out some
variables. The printouts on a test machine showed ZERO values, where
there should be non-zero ones (0.008). The original machine, where the
modules were compiled, showed the correct values.

And this all happened silently, without any error during the import of
the module!

What's the reason of this error? I start thinking, that it may be
related to the fact, that the decimal point on the Enlish Windows XP is
the '.' character, and on the Polish one - ','.

This is *extremely* unlikely. Firstly, you are (I understand) talking
about a .pyc file, that was produced on an English Windows box. Even
though the "180.0" and the "0.5" are visible as character strings in
the .pyc file, Python sure doesn't use the locale when it loads a .pyc
file.


If I modify the decimal point setting in the Regional Settings in the
Control Panel to the dot character '.' - everything seems to work fine.
Whenever it is set to the comma ',' - floating point constants, like 0.5
are considered ZERO by the import statement.

Secondly, even if you are talking about a .py file, Python takes
absolutely no notice of the locale when it compiles the .py file.
Polish programmers write "0.5", not "0,5". Read the language reference
manual, section 2.4.5 -- it uses ".", not "whatever the decimal point
character might be in your locale". If it did depend on locale, you
would need a locale declaration at the top of the file, if one wanted
.py files to be portable internationally ; ever seen or heard of such a
declaration?
Right! The language syntax requires to use the dot regardless of the
locale, BUT the constants are written to the .pyc file in a string form,
probably using repr(), WHICH APPARENTLY DEPENDS ON THE LOCALE (!), when
the documentation states, that the built-in float(), str() functions are
locale-unaware (the locale module provides appropriate functions
supporting the locale).

Thirdly, if the dot was interpreted as something other than a decimal
point, then what? Perhaps assign a tuple (0, 5), or perhaps a syntax
error; zero is achieved under what conditions?
No, it is not a problem with possibly using the comma instead of a dot
in the SOURCE - there only a dot can be used. That's clear.

It's more likely that the .pyc file has been damaged somehow. AFAIK
they don't have checksums.
Very unlikely. I have made these test also directly, sharing the folder
with the .pyc files on the LAN, and running the program from the test
machine. Then, the .pyc files were not manipulated at all.

Is there a good method to avoid this kind of problems? How to make such
distributed modules really portable?

Distribute source.


Yes, that's an option, but not in this case :)

Tomasz Lisowski
Jul 19 '05 #4
> This may be relevant to the problems you're seeing:
https://sourceforge.net/tracker/?fun...&group_id=5470

The short story, as the tracker item paints it, is that setting
LC_NUMERIC to anything other than 'C' can give results like the ones you
describe---Python itself should never do this, but third parties code
may.

A web search for python LC_NUMERIC should turn up more about this topic,
probably even some past threads on this mailing list/newsgroup.


You've got the point. My code uses wxLocale class from wxPython, and
sets the wxLANGUAGE_POLI SH locale. After setting this locale, I have
added the statement:

locale.setlocal e(locale.LC_NUM ERIC, "C")

and everything seems to be normal now. I agree with the comments in the
tracker item, that the float, str(), repr() functions should be
locale-independent. We have the functions in the locale module, if
someone needs the locale-dependent string-float conversions.

--
Tomasz Lisowski
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2001
by: Mark | last post by:
See something weird on this result? 632.35 - 632.35 = -1.13686837722E-013 If anyone has seen something similar, please let me know! this a part of the code: $total_due = abs( $order_total ) - abs( $total_paid ); echo( $order_total . " - " . $total_paid . " = " . $total_due );
4
1254
by: Donn Cave | last post by:
I ran into a phenomenon that seemed odd to me, while testing a build of Python 2.4.1 on BeOS 5.04, on PowerPC 603e. test_builtin.py, for example, fails a couple of tests with errors claiming that apparently identical floating point values aren't equal. But it only does that when imported, and only when the .pyc file already exists. Not if I execute it directly (python test_builtin.py), or if I delete the .pyc file before importing it...
5
3754
by: Anton Noll | last post by:
We are using Visual Studio 2003.NET (C++) for the development of our software in the fields digital signal processing and numerical acoustics. One of our programs was working correctly if we are using the Debug-Version of the program, but it fails (or leads to false results) if we are using the Release-Version. After a long debugging session we found out, that our program was working correctly, but the floating point processing...
2
1342
by: Chris | last post by:
Hi, a strange behaviour when working with exceptions : when I divide and integer by 0 will an exception be thrown. OK but, when I divide a double by 0 is no exception thrown ??? How come ? Try it out.
1
1139
by: Chris | last post by:
Hi, a strange behaviour when working with exceptions : when I divide and integer by 0 will an exception be thrown. OK but, when I divide a double by 0 is no exception thrown ??? How come ? Try it out.
17
2079
by: Neil Cerutti | last post by:
The Glk API (which I'm implementing in native Python code) defines 120 or so constants that users must use. The constants already have fairly long names, e.g., gestalt_Version, evtype_Timer, keycode_PageDown. Calls to Glk functions are thus ugly and tedious. scriptref = glk.fileref_create_by_prompt( glk.fileusage_Transcript | glk.fileusage_TextMode, glk.filemode_WriteAppend, 0)
17
1870
by: matevzb | last post by:
I've ran into some fishy code that, at first glance, is buggy, but it seems to work correctly and none of the compilers I've tried (five so far, on various systems) gives any warnings. The code: ============================ #include <stdio.h> void fcn (char *str)
2
2519
by: Leslie Sanford | last post by:
I want to define a set of floating point constants using templates insteand of macros. I'd like to determine whether these constants are floats or doubles at compile time. In my header file, I have this: template<bool DoublePrecision> struct Constants { typedef double SampleType; static const double pi;
8
1346
by: Henrik Skak Pedersen | last post by:
Hi, I have the following very simple code snippet: line 1: float f; line 2: f = 100000.99f; Why is f: 100000.992 after line 2? I would assume it to be 100000.99. Cheers
0
9731
marktang
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...
0
9605
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,...
0
10136
tracyyun
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...
0
9208
agi2029
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...
0
6893
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();...
0
5556
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4342
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.