473,772 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)awakeFrom Nib
{
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.400001525878 91.

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 1336
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.400001525878 91.

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.objec tive-c sapsi <sa************ @gmail.comwrote :
The first output in the console is 40.4 and the second
-40.400001525878 91.

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.objec tive-c sapsi <sa************ @gmail.comwrote :
The first output in the console is 40.4 and the second
-40.400001525878 91.

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
2281
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 scattered all over the place and I haven't been able to find mailing lists relating specifically to python and UIs. I'd like to start using Python for some GUI programming again. I'd like to use Tk, but it seems to be dying a slow death, so it's...
0
298
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 people that like to code. Tedious, repetitive c.l.py threads attract people that like to write tedious, repetitive c.l.py threads." -- Robert Kern Yes, commercial Python training *is* available:
59
2530
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 objects, first call a class method to allocate the memory (typically alloc), and then call an initializer (typically starts with init). Some classes have class methods which perform this behind the scenes, especially classes that create cached,...
0
1793
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 gone about halfway through the book, just translating the tutorials from ObjC to PyObjC as I go, and it's worked out fine. When I need help, I post to the pythonmac-sig list, which is a friendly and helpful bunch. (I would think that iPhone...
0
9620
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
10261
Oralloy
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...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6715
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
5354
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
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...

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.