Thread: C Double Float and printf problem

  1. #16
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    same result
    Only if you are printing it out incorrectly. Are you using %lld? It should be able to take you up to 7540113804746346429 before it wraps around. It can go one iteration higher if you make it unsigned.

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by SlyVixsky View Post
    same result
    That probably means that you forgot to change your printf specification to %lld -- unless you're using windows, and using gcc as your compiler, in which case you're just generally in trouble with the long double/long long types.

  3. #18
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    Quote Originally Posted by tabstop View Post
    That probably means that you forgot to change your printf specification to %lld -- unless you're using windows, and using gcc as your compiler, in which case you're just generally in trouble with the long double/long long types.
    yes, windows and gcc

  4. #19
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    yes, windows and gcc
    I'm again going to suggest you use GMP.

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by SlyVixsky View Post
    yes, windows and gcc
    Then to print a long long you need to use %I64d instead of %lld because Microsoft is Just That Way.

  6. #21
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    %I64d yields the same result as %lld, which does work as it should, but still has the limit about the 92 iteration before it begins bouncing pos/neg.

    bithub: can you please post a link for GMP use?

  7. #22
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268

  8. #23
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    Quote Originally Posted by bithub View Post
    yay linux
    i dont use linux...
    Last edited by SlyVixsky; 07-30-2009 at 04:02 PM.

  9. #24
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268

  10. #25
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    the windows fork is nothing but linux installation instructions, no use at all...

  11. #26
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by SlyVixsky View Post
    the windows fork is nothing but linux installation instructions, no use at all...
    Maybe you should choose a different hobby -- programming does not seem to suite you. It took me about 15 seconds to find the Windows instructions:

    MS-DOS and MS Windows
    On an MS-DOS system DJGPP can be used to build MPIR, and on an MS Windows
    system Cygwin, DJGPP and MINGW can all be used. All three are excellent ports
    of GCC and the various GNU tools.
    Cygwin Information and Installation
    DJGPP
    MinGW | Minimalist GNU for Windows
    In addition, project les for MSVC are provided, allowing MPIR to build on Microsoft's
    compiler. This support is provided by Brian Gladman.
    Microsoft also publishes an Interix \Services for Unix" which can be used to build
    MPIR on Windows (with a normal `./configure'), but it's not free software.
    MS Windows DLLs
    On systems `*-*-cygwin*', `*-*-mingw*' and `*-*-pw32*' by default MPIR builds
    only a static library, but a DLL can be built instead using
    ./configure --disable-static --enable-shared
    Static and DLL libraries can't both be built, since certain export directives in
    `mpir.h' must be di erent.
    A MINGW DLL build of MPIR can be used with Microsoft C. Libtool doesn't
    install a `.lib' format import library, but it can be created with MS lib as follows,
    and copied to the install directory. Similarly for `libmp' and `libmpirxx'.
    cd .libs
    lib /def:libgmp-3.dll.def /out:libgmp-3.lib
    MINGW uses the C runtime library `msvcrt.dll' for I/O, so applications wanting
    to use the MPIR I/O routines must be compiled with `cl /MD' to do the same. If
    one of the other C runtime library choices provided by MS C is desired then the
    suggestion is to use the MPIR string functions and con ne I/O to the application.

  12. #27
    Registered User
    Join Date
    Jul 2009
    Posts
    36
    Quote Originally Posted by SlyVixsky View Post
    I have been searching the forum for over an hour trying to find a solution to what i'm seeing, as well as the rest of the internet, but theres nothing i can find.
    I've written two articles that explain your problem: Print Precision of Dyadic Fractions Varies by Language - Exploring Binary and
    Print Precision of Floating-Point Integers Varies Too - Exploring Binary .

    What's happening is that your compiler is not printing enough digits. Surprisingly, compilers vary in how many they print.

    Look at bithub's gcc output and notice that he gets more digits than you.

    (BTW, I get the same "truncated" results that you get when I run your code on Visual C++.)

    P.S. Those articles I cited only explain why you see trailing 0s and not "garbage digits." You still have the problem that your values are larger than what a double can hold accurately. Try long longs or GMP as others have suggested.
    Last edited by DoctorBinary; 07-30-2009 at 08:40 PM. Reason: Added P.S. at bottom

  13. #28
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    Quote Originally Posted by DoctorBinary View Post
    You still have the problem that your values are larger than what a double can hold accurately. Try long longs or GMP as others have suggested.
    I've used long long, and it still cant get past the 93rd iteration of the sequence.

    Cycles 90 to 100 using long long and %lld
    Code:
    2880067194370816120
    4660046610375530309
    7540113804746346429
    -624658365858767487
    1293530146158671551
    -495305351242900332
    -365952336627033177
    -861257687869933510
    6174643828739884737
    -243793304995945036
    3736710778780434371

  14. #29
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    modifying my code by adding the following line

    Code:
    printf( "%d\n", sizeof(y));
    following the second print statement, the new output is showing every number only being allotted 8 bytes, which although correct, does not explain why the display is stopping at 16 digits and buffering the rest with zeros. Changing the coding from double to long double changes this to 12 bytes as expected, however the printf is still only showing 16 digits, then zeros to the decimal point again. Does anyone have any further suggestions?

  15. #30
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by SlyVixsky View Post
    modifying my code by adding the following line

    Code:
    printf( "%d\n", sizeof(y));
    following the second print statement, the new output is showing every number only being allotted 8 bytes, which although correct, does not explain why the display is stopping at 16 digits and buffering the rest with zeros. Changing the coding from double to long double changes this to 12 bytes as expected, however the printf is still only showing 16 digits, then zeros to the decimal point again. Does anyone have any further suggestions?
    Windows does not have the capability to print a long double, as a search on this very board would have shown you. You can print the mantissa and exponent yourself if you want.

    At this point you should probably just write your own 16-byte bignum class and be done with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  3. Noob: Structure problem
    By Karmachrome in forum C Programming
    Replies: 6
    Last Post: 11-10-2005, 06:13 PM
  4. whats wrong with this?
    By petedee in forum C Programming
    Replies: 32
    Last Post: 01-06-2004, 10:28 PM
  5. error in program????
    By SpEkTrE in forum C Programming
    Replies: 5
    Last Post: 11-24-2003, 06:16 PM