i wrote a program that gets the product of 5 bytes and stores it
into a long double however when the number gets large it stores
a negative number which corrupts my data. Is there a way i can cast it unsigned? or do i have to convert it to possitive myself by performing 2's complement or something? Don't know how to do that either using c++... :(
please help me :P
There's no such thing as an unsigned double. However I don't know how you're managing to overflow a double with the product of 5 bytes. Are you sure it's not producing a small number with a negative exponent rather than a negative number?
i'm not sure. what i have is 5 unsigned chars which are read from a file. then (c1*c2*c3*c4*c5) is stored in the long double sum.
then if sum>max; max=sum; (both are same type)
ok what is weird is if i declare them both unsigned long ints then the max is greater then when i declare them long double??
and when i declare them unsigned long there is no negative sums however max is not what its suppose to be because its larger then unsigned long int. anyone know what kind of paradox is occuring here if its not overflowing :P
You need to cast your variables to long doubles.
sum = (long double) c1 * (long double) c2 * (long double) c3 * (long double) c4 * (long double) c5;
or maybe just casting the first is good enough:
sum = (long double) c1 * c2 * c3 * c4 * c5;
I think you can use the <static cast> method, but I'm not sure of the syntax.