Re: Float to String

From: David C. Hoos (david.c.hoos.sr_at_ada95.com)
Date: 11/08/04

  • Next message: David C. Hoos: "Re: Float to String"
    Date: Mon, 8 Nov 2004 14:13:54 -0600
    To: "Pascal Obry" <pascal@obry.net>
    
    

    This is closely-related to a problem I encountered something over
    ten years ago.

    The problem in that case was that values stored in an Oracle database
    when later extracted sometimes differed in the lsb.

    What makes the problem similar is that Oracle required the data to be
    stored in decimal format. The problem with that is that decimal numbers
    are not, in general, exactly represent in binary. E.g. the decimal
    number 0.1 is a non-terminating binary number.

    The good news is that since binary numbers in a machine per force have
    a finite length, they can all be exactly represent as decimal numbers.
    The bad news about this is that the Text_IO.Float_IO package
    implementations I have seen are not capable of outputting those exact
    representations, and we, therefore, were obliged to implement our
    own, in order to satisfy the requirements of our customer to have
    equivalence down to the last bit.

    For binary numbers having n bits following the binary point, n digits
    are required following the decimal point to exactly represent the
    fractional part of the value. Such exact representations will always
    terminate with the digit "5."

    For binary numbers having n bits preceding the binary point, the
    number of decimal digits required to exactly represent the whole-
    number part of the value is (n log2 (10)) + 1.

    I hope this helps.

    ----- Original Message -----
    From: "Pascal Obry" <pascal@obry.net>
    Newsgroups: comp.lang.ada
    To: <comp.lang.ada@ada-france.org>
    Sent: Monday, November 08, 2004 11:06 AM
    Subject: Float to String

    >
    > I was wondering if there is a way to save a float into a string and read
    it
    > back without loosing precision ?
    >
    > I tried something like:
    >
    > F_Str : String (1 .. Float'Base'Digits);
    >
    > Float_Text_IO.Put (F_Str, My_Float, Aft => Float'Base'Digits);
    >
    > But this is not enough... Ideas ?
    >
    > Thanks,
    > Pascal.
    >
    > --
    >
    > --|------------------------------------------------------
    > --| Pascal Obry Team-Ada Member
    > --| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
    > --|------------------------------------------------------
    > --| http://www.obry.org
    > --| "The best way to travel is by means of imagination"
    > --|
    > --| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595
    > _______________________________________________
    > comp.lang.ada mailing list
    > comp.lang.ada@ada-france.org
    > http://www.ada-france.org/mailman/listinfo/comp.lang.ada
    >


  • Next message: David C. Hoos: "Re: Float to String"

    Relevant Pages

    • Re: Converting floats to Strings and back
      ... > I'm trying to convert a String to a float, do some arithmetic on it, then ... > The code works fine until I exceed 8 digits. ... the nasty things that happen with floating point, ...
      (comp.lang.java.programmer)
    • Re: Digits after the decimal point
      ... It struck me when we were being taught about a program which counts the ... number of digits in a given number. ... want to accept it in float datatype. ... You can read it into a string first, ...
      (comp.lang.c)
    • Re: Convert float to double - weird failure
      ... I am trying to cast a float to a double and am using the ... static cast is not correct. ... When you trunc that string at 24 bits (including the initial ... more "0" bits and convert that string to 12 digits), ...
      (comp.programming)
    • Re: Float to String
      ... representations, and we, therefore, were obliged to implement our ... For binary numbers having n bits following the binary point, n digits ... Subject: Float to String ...
      (comp.lang.ada)
    • Re: Check before insert in Database
      ... I need to get a string and I would like to make sure ... to be a letter followed by 5 digits, ... It allows signs, decimal points, and exponential notation. ... The point is I have to insert the data in an Oracle database. ...
      (comp.lang.php)