• orca/c strtod

    From vavruska@gmail.com@21:1/5 to All on Fri Apr 16 20:20:25 2021
    this simple code snippet

    char a[] ="1";
    char *end;
    double val;

    val = strtod(a, &end);

    printf("%f %d\n", val, atoi(a));

    prints out
    6217.000000 1

    does strtod not work or am I doing something wrong?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Schmidt@21:1/5 to vavr...@gmail.com on Sat Apr 17 10:42:29 2021
    On 4/16/21 11:20 PM, vavr...@gmail.com wrote:
    this simple code snippet

    char a[] ="1";
    char *end;
    double val;

    val = strtod(a, &end);

    printf("%f %d\n", val, atoi(a));

    prints out
    6217.000000 1

    does strtod not work or am I doing something wrong?

    Sitting at my Linux box, this code works as expected:

    #include <stdio.h>
    #include <stdlib.h>

    void main()
    {
    char a[] ="1";
    char *end;
    double val;

    val = strtod(a, &end);

    printf("%f %d\n", val, atoi(a));
    }

    But I find if I omit <stdlib.h> output get a little wonky (and I have
    attendant compiler warnings). Are you including that?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From vavruska@gmail.com@21:1/5 to schmidtd on Sat Apr 17 11:41:24 2021
    including the stdlib.h did help with the code but am still having issues with doubles in my main project even with including the stdlib. I have no real need for floating point but I am using an open source library.



    On Saturday, April 17, 2021 at 10:42:31 AM UTC-4, schmidtd wrote:
    On 4/16/21 11:20 PM, vavr...@gmail.com wrote:
    this simple code snippet

    char a[] ="1";
    char *end;
    double val;

    val = strtod(a, &end);

    printf("%f %d\n", val, atoi(a));

    prints out
    6217.000000 1

    does strtod not work or am I doing something wrong?
    Sitting at my Linux box, this code works as expected:

    #include <stdio.h>
    #include <stdlib.h>

    void main()
    {
    char a[] ="1";
    char *end;
    double val;

    val = strtod(a, &end);

    printf("%f %d\n", val, atoi(a));
    }
    But I find if I omit <stdlib.h> output get a little wonky (and I have attendant compiler warnings). Are you including that?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kelvin Sherlock@21:1/5 to All on Sun Apr 18 00:02:30 2021
    Use #pragma lint 7 (or -1) to catch missing functions and prototypes.

    -------
    ProLine: kelvin@pro-kegs

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From thefadden@gmail.com@21:1/5 to Kelvin Sherlock on Sun Apr 18 07:55:18 2021
    On Saturday, April 17, 2021 at 6:20:10 PM UTC-7, Kelvin Sherlock wrote:
    Use #pragma lint 7 (or -1) to catch missing functions and prototypes.

    I didn't really appreciate the value of function prototypes in C until I was trying to call malloc() on a IIgs. Without the prototype it assumed the function returned a 16-bit int instead of a 32-bit pointer, and things didn't go well when I tried to
    dereference the result.

    Since "double" is wider than 16 bits, every function that uses it as an argument or result needs a prototype. Turning up the warnings is a very good idea.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From vavruska@gmail.com@21:1/5 to thef...@gmail.com on Sun Apr 18 11:32:53 2021
    I usually have lint -1 for compiles. I was running into the issue in a larger project where I did have lint -1 so I wrote quick little test program to test it out where I forgot the lint. While the little test program works the large project does not.
    The following even does not work correct

    int x=1;
    double d =x;

    d winds up being some large number. Does anyone need to know if you need to startup SANE to use doubles?



    On Sunday, April 18, 2021 at 10:55:18 AM UTC-4, thef...@gmail.com wrote:
    On Saturday, April 17, 2021 at 6:20:10 PM UTC-7, Kelvin Sherlock wrote:
    Use #pragma lint 7 (or -1) to catch missing functions and prototypes.
    I didn't really appreciate the value of function prototypes in C until I was trying to call malloc() on a IIgs. Without the prototype it assumed the function returned a 16-bit int instead of a 32-bit pointer, and things didn't go well when I tried to
    dereference the result.

    Since "double" is wider than 16 bits, every function that uses it as an argument or result needs a prototype. Turning up the warnings is a very good idea.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From thefadden@gmail.com@21:1/5 to vavr...@gmail.com on Mon Apr 19 12:36:45 2021
    On Sunday, April 18, 2021 at 11:32:54 AM UTC-7, vavr...@gmail.com wrote:
    [...] The following even does not work correct

    int x=1;
    double d =x;

    d winds up being some large number. Does anyone need to know if you need to startup SANE to use doubles?

    Are you confident 'd' is being displayed correctly? i.e. does "d = 1.0" work, but "d = x" does not? Does it work correctly if you cast it to float before passing it to printf()?

    Your original example assumed that strtod() was failing... are you sure the problem isn't with printf()?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kelvin Sherlock@21:1/5 to vavr...@gmail.com on Tue Apr 20 00:24:45 2021
    runtime int-to-double conversion uses SANE (FI2X then FX2D).

    If SANE wasn't loaded I would expect it to crash due to stack imbalance
    (since SANE isn't there to clean up the stack).

    In <c673a52a-20ed-4592-880b-68cba62f9302n@googlegroups.com>
    "vavr...@gmail.com" <vavruska@gmail.com> writes:

    I usually have lint -1 for compiles. I was running into the issue in a larger >project where I did have lint -1 so I wrote quick little test program to >test it out where I forgot the lint. While the little test program works
    the large project does not. The following even does not work correct

    int x=1;
    double d =x;

    d winds up being some large number. Does anyone need to know if you need
    to startup SANE to use doubles?



    On Sunday, April 18, 2021 at 10:55:18 AM UTC-4, thef...@gmail.com wrote:
    On Saturday, April 17, 2021 at 6:20:10 PM UTC-7, Kelvin Sherlock wrote:
    Use #pragma lint 7 (or -1) to catch missing functions and prototypes.
    I didn't really appreciate the value of function prototypes in C until
    I was trying to call malloc() on a IIgs. Without the prototype it assumed
    the function returned a 16-bit int instead of a 32-bit pointer, and things >didn't go well when I tried to dereference the result.

    Since "double" is wider than 16 bits, every function that uses it as an >argument or result needs a prototype. Turning up the warnings is a very good >idea.

    -------
    ProLine: kelvin@pro-kegs

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)