944,135 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 12350
  • C RSS
Feb 15th, 2007
0

floating point : overflow error

Expand Post »
Hi!
can somebody please tell me how to avoid floating point overflow error? Mine is a very big program that uses double data type.
Is there any way in which I can roundoff double numbers to upto certain digit?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Harshita_garg is offline Offline
10 posts
since Nov 2006
Feb 15th, 2007
0

Re: floating point : overflow error

get keyboard input as a string then do whatever you want with the individual digits.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Feb 16th, 2007
0

Re: floating point : overflow error

get keyboard input as a string then do whatever you want with the individual digits.
He didn't specify that the floating point number was inputed off the command line, but the string idea will work nonetheless as long as you enumerate through the string until you reach the "." character after which you can convert the character(s) number(s) into an integer and proceed to the logical operations that determine if a one should be added or not.

However, if you're true reason for rounding is so that you have a percise number to output than you should wait until you're about to output it and can use, but no limited, to one of the following:

// If you want to display 2 digits passed the decimal point
C - printf("%.2f", floatingNumber);

// same as above but using the C++ standard library
C++ - cout << "Set percision: " << setpercision(number) << DoubleNumber;

// More info: http://msdn2.microsoft.com/en-us/library/wyk4d9cy.aspx
Math.round(double number); // Round to the nearest integer

I've go to go to work, I'll post a function that will round a double number when I return.

Good luck, LamaBot
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Feb 17th, 2007
0

Re: floating point : overflow error

Back from work...

Here is a simple program that'll illustrate how to use strings to convert floating point values (represented as a string) to an integer:

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int DoubleRound(char *fnum);
  5. int main() {
  6. char dnum[] = "92.45972340958723";
  7. printf("%d",Double2Integer(dnum));
  8. return 0;
  9. }
  10. int Double2Integer(char *fnum) {
  11. int integeri = atoi(fnum),i,j,k;
  12. char hex = 0x30;
  13. i=k=0;
  14.  
  15. while (fnum[i] != '.') i++;
  16. j = strlen(fnum);
  17. for (;j>i;j--) {
  18. switch(fnum[j]) {
  19. case '5':
  20. case '6':
  21. case '7':
  22. case '8':
  23. case '9':
  24. while (fnum[j-1] != hex)
  25. hex++;
  26. hex++;
  27. fnum[j-1] = hex;
  28. break;
  29. }
  30. }
  31. switch(fnum[i+1]) {
  32. case '5':
  33. case '6':
  34. case '7':
  35. case '8':
  36. case '9':
  37. integeri++;
  38. break;
  39. }
  40. return integeri;
  41. }

Please not that the code is an illustration but might not be considered good programming in some views. I hope I helped.

Good luck, LamaBot
Last edited by Lazaro Claiborn; Feb 17th, 2007 at 12:39 am. Reason: formatting
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: How to get status of a process
Next Thread in C Forum Timeline: bioskey()





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC