Thread: Problems with doubles

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    10

    Problems with doubles

    Hi, I am making a algorithm that is very important the value of the numbers, but I have some problems, for example I need to do this operation: 1.0/14709204.0, after for I know the result of the operation I write the result of operation in one file, for example I do this:
    Code:
    FILE* tot = fopen("total.txt", "wb");
    char buffft [2000];
    sprintf (buffft,"%f",1/14709204);
    fwrite(buffft,1,strlen(buffft),tot);
    After I open the file total.txt I can see 0.000000, and the correct result is :6.798464417*10-8, I need this result, can someone help me please?any idea?
    Thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >1/14709204
    This is an integer expression with an integer result because both operands are integers. To make it floating-point, change one or both of the values to double:
    Code:
    sprintf (buffft,"%f",1.0/14709204.0);
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    10
    Gracias por la respuesta, pero hago esto que usted cuenta y el resultado es el mismo: 0.000.
    ¿Alguna idea?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Hablar por favor inglés.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    10
    I have written it in Spanish, this one is the translation:
    Thanks for the reply, but I do this that you tell and the result is the same: 0.000.
    Any idea?

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Try the %g format modifier instead of %f.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    10
    Thanks, now in the file I can see 6.79846e-008 (the correct result), Therefore now I that the result of my operations they are correct.. Therfore the problem of my algorithm are in the other part...jeje thanks for the fast replys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Problems with Summing lots of doubles
    By firetheGlazer in forum C Programming
    Replies: 7
    Last Post: 07-15-2008, 04:44 AM
  3. parse doubles from socket read?
    By willy in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:32 AM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. preventing rounding problems with doubles
    By mccoz in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2004, 09:23 AM