Thread: -1.#J outout of sprintf

  1. #1
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788

    -1.#J outout of sprintf

    I'm using sprintf to convert some struct with doubles into string

    I'm using format "%.2f"

    Code:
    double value;
    printf("%.2f", value);
    Sometimes output is presented as following
    Code:
    -1.#J
    I could not understand what can cause this type of output.
    Could you link me to some reference?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It's a malformed floating point number. I'm not sure exactly what the #XX codes signify [and it's probably not much help anyways whether the number is "not a number" or "bad number" in some other way].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    ok thanks

    As a result of the above - another question
    Code:
    struct a
    {
       double b;
       double c;
       int d;
    };
    
    struct a array[2][2] = {{{0}}};
    I'd like to be sure that this code initializes all members of all 4 structs to zeros.

    If not - how can I initialize all strcu members in the array above to zeroes in a portable way?
    I'd like to avoid running loops on all struct members (there is a lot more than 3)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, a struct and/or array will be filled to completion with zero if you do not supply a full set of initializer values.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    Quote Originally Posted by vart View Post
    ok thanks

    As a result of the above - another question
    Code:
    struct a
    {
       double b;
       double c;
       int d;
    };
    
    struct a array[2][2] = {{{0}}};
    I'd like to be sure that this code initializes all members of all 4 structs to zeros.
    Yes, and you don't need the extra curly brackets.
    Code:
    struct a array[2][2] = {0};

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Banana Man View Post
    Yes, and you don't need the extra curly brackets.
    Code:
    struct a array[2][2] = {0};
    That what I had - And got the above results with printf... so I'm not so sure now...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So have you verified that the value is actually zero?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    That what I had - And got the above results with printf... so I'm not so sure now...
    What does this program print?
    Code:
    #include <stdio.h>
    
    struct a
    {
       double b;
       double c;
       int d;
    };
    
    int main()
    {
        struct a array[2][2] = {0};
        int i;
        int j;
    
        for ( i = 0; i < 2; ++i )
        {
            for ( j = 0; j < 2; ++j )
            {
                struct a *x = &array[i][j];
    
                printf("(%f,%f,%d)\t", x->b, x->c, x->d);
            }
    
            puts("");
        }
    
        return 0;
    }

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by matsp View Post
    So have you verified that the value is actually zero?

    --
    Mats
    I'd like to...

    How could I add assert on value that is printed as -1.#J?

    I will add some asserts after initialization of the struct - to check if some values are not 0.
    But I'd like to add some assert inside my calculations to catch the case when the double value gets corrupted...

    Could I do it?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In C99, you've got some extra-fancy "is" tests, I think; there's "isnan", or even "fpclassify".

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by vart View Post
    I'd like to...

    How could I add assert on value that is printed as -1.#J?
    Quick and dirty:

    Code:
    char buf[16];
    snprintf(buf, 16, "%.2f", value);
    if(strcmp(buf, "-1.#J") == 0)
    {
        /* Caught it */
    }
    That kind of sucks but it might be a good short-term method.

    Looking at -1.#J, I almost want to say that it's trying to print an imaginary number, which would imply some kind of dramatic memory corruption affecting the "%.2f" string literal, but that seems far-fetched.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I'm using C89 on VS6.0 + Windows SDK
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by vart View Post
    I'm using C89 on VS6.0 + Windows SDK
    In that case, replace my call to snprintf() with _snprintf()

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by brewbuck View Post
    Quick and dirty:
    Thanks, It seems - I'll implement this solution...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sprintf overflows my buffer -- why?
    By Lasston in forum C Programming
    Replies: 26
    Last Post: 06-20-2008, 04:33 PM
  2. sprintf : garbage appended
    By yeller in forum C Programming
    Replies: 9
    Last Post: 12-17-2007, 10:21 AM
  3. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  4. sprintf in C and C++
    By usu_vlsi in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2005, 04:14 AM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 01:35 PM