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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sapsi

    #1

    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

  • Gabriel Genellina

    #2
    Re: Using Python from Cocoa App via PyObjc - numbers dontmatch...

    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

    Comment

    • Michael Ash

      #3
      Re: Using Python from Cocoa App via PyObjc - numbers dont match...

      In comp.lang.objec tive-c sapsi <saptarshi.guha @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:



      --
      Michael Ash
      Rogue Amoeba Software

      Comment

      • sapsi

        #4
        Re: Using Python from Cocoa App via PyObjc - numbers dont match...

        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 <saptarshi.guha @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:
        >

        >
        --
        Michael Ash
        Rogue Amoeba Software

        Comment

        Working...