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

S

sapsi

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
 
G

Gabriel Genellina

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
 
M

Michael Ash

In comp.lang.objective-c sapsi said:
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
 
S

sapsi

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 said:
In comp.lang.objective-c sapsi said:
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
 
D

Dennis Lee Bieber

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?
Python floats are C double precision, as I recall. Python uses the
C-runtime for the built-in data types, so passing them is no problem. It
is only when you convert between binary and textual representations that
you cause "losses"
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

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,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top