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:

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:

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

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:

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:

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

-2147483648 = 0x80000000 (in hexadecimal), which is the smallest value that an integer can hold. An integer's range is from -2147483648 to 2147483647. I don't know how the computer handles it when you try to hand it a value that is outside of its legal range, but it looks to me like it probably set an overflow flag and gave up when it tried to convert a double (which can handle larger numbers than an integer, I believe) into an integer. There are libraries/data types out there that can handle larger integers than 2147483647, though I don't know which they are (possibly the boost libraries?), but clearly the reason for the -2147483648 is due to overflow. The computer simply couldn't store an integer that large.

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.

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.

#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) ;
}

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/

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.

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....

> 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/Floating_point#Accuracy_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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.