-1.#J outout of sprintf

This is a discussion on -1.#J outout of sprintf within the C Programming forums, part of the General Programming Boards category; I'm using sprintf to convert some struct with doubles into string I'm using format "%.2f" Code: double value; printf("%.2f", value); ...

  1. #1
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484

    -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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I'm using C89 on VS6.0 + Windows SDK
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by brewbuck View Post
    Quick and dirty:
    Thanks, It seems - I'll implement this solution...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 09: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, 03:14 AM
  5. Sprintf
    By Trauts in forum C++ Programming
    Replies: 10
    Last Post: 01-15-2003, 12:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21