| | |
int(variable) giving illogical numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 6
Reputation:
Solved Threads: 0
I'm having some trouble getting the shortened integer value of a fairly long double value. The double value in question is "1234567890123457024". When taking the int of it, such as:
This value is giving me a result of -2147483648, which is completely illogical due first to the fact that this is a negative number where the double number is positive. Any ideas on what is causing this and/or how to fix this?
Also, I tried another approach, since I'm trying to extract each digit out of this long number, I used the code:
When printout out the variables as they get to them, it is able to print out the first half of the variables (the last half printed out) up to 7890, but when the number is very large, taking the int mod of it just returns -8, which is again illogical and causing me several errors.
Are these problems primarily due to the fact that the numbers used are so large or what? Also, any ideas on how to go about extracting the digits if it is impossible to do this using the int() command?
Thanks for your help
C++ Syntax (Toggle Plain Text)
int(value);
This value is giving me a result of -2147483648, which is completely illogical due first to the fact that this is a negative number where the double number is positive. Any ideas on what is causing this and/or how to fix this?
Also, I tried another approach, since I'm trying to extract each digit out of this long number, I used the code:
C++ Syntax (Toggle Plain Text)
int variable = int(tempInt)%10; tempInt /= 10;
When printout out the variables as they get to them, it is able to print out the first half of the variables (the last half printed out) up to 7890, but when the number is very large, taking the int mod of it just returns -8, which is again illogical and causing me several errors.
Are these problems primarily due to the fact that the numbers used are so large or what? Also, any ideas on how to go about extracting the digits if it is impossible to do this using the int() command?
Thanks for your help
•
•
Join Date: Jan 2008
Posts: 3,837
Reputation:
Solved Threads: 503
•
•
•
•
I'm having some trouble getting the shortened integer value of a fairly long double value. The double value in question is "1234567890123457024". When taking the int of it, such as:
C++ Syntax (Toggle Plain Text)
int(value);
This value is giving me a result of -2147483648, which is completely illogical due first to the fact that this is a negative number where the double number is positive. Any ideas on what is causing this and/or how to fix this?
Also, I tried another approach, since I'm trying to extract each digit out of this long number, I used the code:
C++ Syntax (Toggle Plain Text)
int variable = int(tempInt)%10; tempInt /= 10;
When printout out the variables as they get to them, it is able to print out the first half of the variables (the last half printed out) up to 7890, but when the number is very large, taking the int mod of it just returns -8, which is again illogical and causing me several errors.
Are these problems primarily due to the fact that the numbers used are so large or what? Also, any ideas on how to go about extracting the digits if it is impossible to do this using the int() command?
Thanks for your help
Regarding how to extract the digits, if you are storing the number as a double, it is possible that you are not storing the exact number and you have a roundoff error. Thus if you need the exact digits of the number, that might be impossible. Depending on your application, you may want to research data types that can provide more than 32 bits to store an integer. Like I said, I don't know which libraries these are, but I'm almost positive they are out there somewhere.
Last edited by VernonDozier; May 3rd, 2008 at 3:17 am.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
one way to extract the digits from a number is to convert the number to a string and then convert the characters in the string to digits.
as Dozier pointed out, if what you really want is an integral value with a large number of digits, using a double is not a good idea. here are two C++ classes you could use:
class ZZ from NTL http://www.shoup.net/ntl/doc/tour-ex1.html
class bigint from LiDIA http://www.cdc.informatik.tu-darmstadt.de/TI/LiDIA/
c++ Syntax (Toggle Plain Text)
#include <sstream> #include <vector> #include <functional> template< typename NUMERIC_TYPE > std::vector<int> get_digits( const NUMERIC_TYPE& number ) { // invariant: number is non-negetive std::ostringstream stm ; stm.precision(0) ; stm << std::fixed << number ; const std::string& str = stm.str() ; std::vector<int> digits( str.begin(), str.end() ) ; std::transform( digits.begin(), digits.end(), digits.begin(), std::bind2nd( std::minus<int>(), stm.widen('0') ) ) ; return digits ; } int main() { double value = 1234567890123457024.0 ; std::vector<int> digits = get_digits(value) ; }
class ZZ from NTL http://www.shoup.net/ntl/doc/tour-ex1.html
class bigint from LiDIA http://www.cdc.informatik.tu-darmstadt.de/TI/LiDIA/
Last edited by vijayan121; May 3rd, 2008 at 3:54 am.
•
•
Join Date: Apr 2008
Posts: 6
Reputation:
Solved Threads: 0
yea i know the string to digit way is possible, in fact thats basically what i did before that. i converted the long number from a string to a double, but then the function i'm writing must use the double value to convert that back to a string again. so a double value containing the number must be used to convert it back to a string.
If this was an assignment, then my guess is it's a trick thing and probably you're expected to write your own class that can represent a huge int like the one you have (exactly like what someone else has already done in links from Vijayan).
In short there is no built-in mechanism to handle integers of this size, it has to be a user defined class.
In short there is no built-in mechanism to handle integers of this size, it has to be a user defined class.
Are you Agile.. ?
•
•
Join Date: Apr 2008
Posts: 6
Reputation:
Solved Threads: 0
ok i found a way to extract the digits but have encountered another problem. lets say that i have a double number = .3 . When I multiply this by 10, the result should be 3. Then, when taking the integer version of this using int(number), the result should be 3, correct?
However, taking the int results in a value of 2 instead of 3. Any idea on why this is occurring? It should be like this: .3*10 = 3, int(3) == 3. But there seems to be some error in rounding or something....
However, taking the int results in a value of 2 instead of 3. Any idea on why this is occurring? It should be like this: .3*10 = 3, int(3) == 3. But there seems to be some error in rounding or something....
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> ets say that i have a double number = .3 . When I multiply this by 10, the result should be 3.
> Then, when taking the integer version of this using int(number), the result should be 3, correct?
not necessarily. see
http://en.wikipedia.org/wiki/Floatin...uracy_problems
The Art of Computer Programming, Volume 2: Seminumerical Algorithms, Third Edition. by Donald Knuth (Addison-Wesley, 1997). Section 4.2: Floating Point Arithmetic, pages.214–264.
> Then, when taking the integer version of this using int(number), the result should be 3, correct?
not necessarily. see
http://en.wikipedia.org/wiki/Floatin...uracy_problems
The Art of Computer Programming, Volume 2: Seminumerical Algorithms, Third Edition. by Donald Knuth (Addison-Wesley, 1997). Section 4.2: Floating Point Arithmetic, pages.214–264.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Storing text in structure from file
- Next Thread: C++ static class
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






