printf() is rounding floating-point numbers with non-C locale... where is the logic? - HP UX

This is a discussion on printf() is rounding floating-point numbers with non-C locale... where is the logic? - HP UX ; Hi all, We're facing a problem where printf() is rounding floating-point numbers down to the nearest integer when LANG=fr_FR.iso88591. Here is the full story, illustrated by the short program foo.c: #include int main(void) { float f; double d; f = ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: printf() is rounding floating-point numbers with non-C locale... where is the logic?

  1. printf() is rounding floating-point numbers with non-C locale... where is the logic?

    Hi all,

    We're facing a problem where printf() is rounding floating-point
    numbers down to the nearest integer when LANG=fr_FR.iso88591.

    Here is the full story, illustrated by the short program foo.c:

    #include
    int main(void) {
    float f;
    double d;
    f = 0.5;
    printf("f=%f\n", f);
    f = (float)0.5;
    printf("0.5=%f\n", 0.5);
    printf("0.5f=%f\n", 0.5f);
    printf("0.9=%f\n", 0.9);
    printf("1.9=%f\n", 1.9);
    d = 0.5;
    printf("d0.5=%f\n", d);
    printf("1.0/2.0=%f\n", 1.0 / 2.0);
    return 0;
    }

    When foo.c is compiled and ran locally on HP-UX 11.11 (hpux-host),
    everything's fine:

    hpux-host# /usr/bin/cc foo.c
    hpux-host# a.out
    f=0.500000
    0.5=0.500000
    0.5f=0.500000
    0.9=0.900000
    1.9=1.900000
    d0.5=0.500000
    1.0/2.0=0.500000


    When foo.c is compiled remotely on HP-UX 11.11 (hpux-host) from a
    Solaris host (solaris-host)...

    solaris-host# rsh hpux-host /usr/bin/cc foo.c

    .... and then, ran locally on the hpux-host, all floating-point numbers
    are rounded down to the nearest integer (although the decimal part is
    printed using default precision of six digits):

    hpux-host# a.out
    f=0.000000
    0.5=0.000000
    0.5f=0.000000
    0.9=0.000000
    1.9=1.000000
    d0.5=0.000000
    1.0/2.0=0.500000

    After investigating the problem, we discovered the bloblem was caused
    by variable LANG being set to a non-C locale.

    When running non-interactive command remotely on the hpux-host, the
    LANG variable was set to "fr_FR.iso88591":

    solaris-host# rsh hpux-host env | grep -e '^\(LANG\|LC_.*\)='
    LANG=fr_FR.iso88591
    solaris-host#

    .... whereas it was undefined when running commands interactively on
    hpux-host:

    hpux-host# env | grep -e '^\(LANG\|LC_.*\)='
    hpux-host#

    The problem was "solved" by setting LANG to "C".

    However, I cannot understand the logic of rounding floats when LANG is
    "fr_FR.iso88591"...
    Does anyone have an explanation to such a behavior?

    Grégoire


  2. Re: printf() is rounding floating-point numbers with non-C locale... where is the logic?

    gvt wrote:
    : We're facing a problem where printf() is rounding floating-point
    : numbers down to the nearest integer when LANG=fr_FR.iso88591.

    There have been many problems like this in the past but I don't see
    any recent ones for PA, B.11.11.16. What is the version of your compiler?
    what $(whence cc) # if using a "real" shell

    : When foo.c is compiled remotely on HP-UX 11.11 (hpux-host) from a

    Are you sure it is 11.11 and not 11.23?

    : ... and then, ran locally on the hpux-host, all floating-point numbers
    : are rounded down to the nearest integer

    This can happen if the "." is treated as a "," in the locale.

    : After investigating the problem, we discovered the problem was caused
    : by variable LANG being set to a non-C locale.

    For IPF, CR JAGaf96995 caused this problem. This was patched in A.06.12,
    PHSS_34855. aCC1 had this problem a decade ago with CR JAGaa90480.

    : The problem was "solved" by setting LANG to "C".

    Or you can compile with -Y.

    : I cannot understand the logic of rounding floats when LANG is fr_FR.iso88591.
    : Does anyone have an explanation to such a behavior?
    : Gr=E9goire

    Because part of the compiler allows "." in floating point literals.
    And unless special care is taken, strtod(3C) doesn't and just truncates.

  3. Re: printf() is rounding floating-point numbers with non-C locale... where is the logic?


    Dennis Handly a écrit :

    > gvt wrote:
    > : We're facing a problem where printf() is rounding floating-point
    > : numbers down to the nearest integer when LANG=fr_FR.iso88591.
    >
    > There have been many problems like this in the past but I don't see
    > any recent ones for PA, B.11.11.16. What is the version of your compiler?
    > what $(whence cc) # if using a "real" shell


    #what $(whence cc)
    /bin/cc:
    LINT B.11.11.04 CXREF B.11.11.04
    HP92453-01 B.11.11.26792.GP HP C Compiler
    $ Sep 8 2000 23:13:51 $



    > Are you sure it is 11.11 and not 11.23?


    #uname -a
    HP-UX xxxxxx B.11.11 U 9000/800 415524185 unlimited-user license


    > For IPF, CR JAGaf96995 caused this problem. This was patched in A.06.12,
    > PHSS_34855. aCC1 had this problem a decade ago with CR JAGaa90480.
    >
    > : The problem was "solved" by setting LANG to "C".
    >
    > Or you can compile with -Y.
    >

    In my case, the "-Y" option change nothing.

    > Because part of the compiler allows "." in floating point literals.
    > And unless special care is taken, strtod(3C) doesn't and just truncates.


    Thank for yours explanations.


  4. Re: printf() is rounding floating-point numbers with non-C locale... where is the logic?

    locnet2005 wrote:
    : HP92453-01 B.11.11.26792.GP HP C Compiler

    That's very old, April 2002, perhaps it was broken there.
    You need to go to a newer version:
    http://h21007.www2.hp.com/dspp/tech/...1,1255,00.html

    : In my case, the "-Y" option change nothing.

    Ok, it is different technology. I thought we borrowed the fix from them
    in 1996.

+ Reply to Thread