floating point : overflow error

Reply

Join Date: Nov 2006
Posts: 10
Reputation: Harshita_garg is an unknown quantity at this point 
Solved Threads: 0
Harshita_garg Harshita_garg is offline Offline
Newbie Poster

floating point : overflow error

 
0
  #1
Feb 15th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: floating point : overflow error

 
0
  #2
Feb 15th, 2007
get keyboard input as a string then do whatever you want with the individual digits.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: floating point : overflow error

 
0
  #3
Feb 16th, 2007
Originally Posted by Ancient Dragon View Post
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: floating point : overflow error

 
0
  #4
Feb 17th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC