473,408 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Using Python from Cocoa App via PyObjc - numbers dont match...

Hi,
I managed to create a python class and instantiate that from my
Objective C Cocoa App (its not a python app).
Essentially, i made two classes in IB and then another class(ogle) with
outlets for these two.

Now here is the implementation for ogle.m
-(void)awakeFromNib
{
NSNumber *n=[NSNumber numberWithFloat:40.4];
NSLog(@"%@",n);

NSNumber *b=[it2 printNok:n];
NSLog(@"%@",b);

//
}
The first output in the console is 40.4 and the second
-40.40000152587891.

If i change the NSLog(s) to "%f",[n floatValue] (and the second
likewise) the first is 40.400002 and the second is -40.400002.

Why does this happen?
Thanks
Saptarshi

p.s the python routine printNok, just returns the negative of the
number i.e printNok:

def printNok_(self, obj):
return -obj

Nov 9 '06 #1
3 1304
At Wednesday 8/11/2006 22:22, sapsi wrote:
>I managed to create a python class and instantiate that from my
Objective C Cocoa App (its not a python app). [...]
The first output in the console is 40.4 and the second
-40.40000152587891.

If i change the NSLog(s) to "%f",[n floatValue] (and the second
likewise) the first is 40.400002 and the second is -40.400002.

Why does this happen?
This is mostly independent on the language in use.
Numbers like 40.4 don't have, in general, an exact binary representation.
See http://docs.python.org/tut/node16.html
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
Nov 9 '06 #2
In comp.lang.objective-c sapsi <sa************@gmail.comwrote:
The first output in the console is 40.4 and the second
-40.40000152587891.

If i change the NSLog(s) to "%f",[n floatValue] (and the second
likewise) the first is 40.400002 and the second is -40.400002.
I assume you are complaining about the fact that it's not printing exactly
40.4, and wondering where the error is coming from. (It helps if you
actually state this yourself, so we don't have to assume it, rather than
just laying out the circumstances.)

The answer is that floating point numbers are inherently imprecise. Read
through this essential resource:

http://docs.sun.com/source/806-3568/ncg_goldberg.html

--
Michael Ash
Rogue Amoeba Software
Nov 9 '06 #3
Hi,
Well not a complaint as such but a question. Thank you for the response
however and the link. I have seen it before but never got around to
reading it...

Further to this, if i was writing a python module and c function and
suppose the python module and the c function called each other back
and forth exchanging results from float calculations - would not the
results then become flawed? How would one pass floats between different
languages - using special purpose data structures understood by both
the python module and c code?

I asked this question without reading the link, so if it stinks of
ignorance please forget it.

Thank you
Saptarshi
Michael Ash wrote:
In comp.lang.objective-c sapsi <sa************@gmail.comwrote:
The first output in the console is 40.4 and the second
-40.40000152587891.

If i change the NSLog(s) to "%f",[n floatValue] (and the second
likewise) the first is 40.400002 and the second is -40.400002.

I assume you are complaining about the fact that it's not printing exactly
40.4, and wondering where the error is coming from. (It helps if you
actually state this yourself, so we don't have to assume it, rather than
just laying out the circumstances.)

The answer is that floating point numbers are inherently imprecise. Read
through this essential resource:

http://docs.sun.com/source/806-3568/ncg_goldberg.html

--
Michael Ash
Rogue Amoeba Software
Nov 9 '06 #4

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

Similar topics

3
by: Kenneth McDonald | last post by:
If this is not an appropriate newsgroup for this type of posting, please let me know and (if possible) suggest an alternative. I've done a fair bit of research on the net, but information is...
0
by: Cameron Laird | last post by:
QOTW: "It seems to me that Java is designed to make it difficult for programmers to write bad code, while Python is designed to make it easy to write good code." -- Magnus Lycka "Code attracts...
59
by: Kevin Walzer | last post by:
From the introduction to PyObjC, the Python-Objective-C bridge on Mac OS X: "As described in Objective-C for PyObjC users the creation of Objective-C objects is a two-stage process. To initialize...
0
by: Joe Strout | last post by:
On Nov 4, 2008, at 10:33 AM, Michael Torrie wrote: I've found "Cocoa Programming for Mac OS X", in combination with the PyObjC tutorials on the web, to be a very accessible combination. I've...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
isladogs
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...
0
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...

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.