# Thread: Stupid error with long and double types

1. ## Stupid error with long and double types

Hello everybody.
I work in c++ for a long time, but this never happen to me before.
I have a long variable, let's say
long x;
then i try to assign it with
x=36*1000 (this is only an example. in fact, i use other variables)

Guess what? after this line, x=-29536 !!! Why???

Same if i use double type.

I really don't understand.

2. Wow. You really will have to show some real code with that one. It's just... not possible. Unless your compiler is under some Fool's Day spell.

3. You're likely assigning a value greater than a signed long can hold and the value is wrapping around to the negatives, again. However, if that's the case I'm sure the value you assigned wasn't 36*1000 which isn't even close to large enough. I honestly don't understand why people think writing random crap and asking why their code doesn't work is a good idea.

4. Hello
C'mon, i am not that stupid and i know what i am doing. The closest to the truth is the answer with the Fool's day spell.

Trust me, i know what i am doing.
Further details.
i wanted to calculate the number of seconds from a given hour of the day. Crap, simple no?
So...

double number_seconds;
number_seconds=t.ti_hour*3600;
[......]

Actualy, i was trying to calculate the number of seconds from a given number of years, but the code above is the part that gives errors.
So first i run it and i see negative value for number_seconds. I thought it's too much for double type. Then i start debugging. When i get to the lines above, t.ti_hour is 14 in my case, and it shows in the debugger a negative value for number_seconds.
Here is the interesting part: i create new program:

void main()
{
double x;
x=14*3600;
printf("%.0f",x);
}
guess what? Negative value again.
I tried
x=360000;
and it worked.
Then again
x=10*3600;
and again negative value.
Believe me or not, that was exactly what happened.

5. You gotta learn to use code tags man. Anyway, what is your compiler and OS?

...and while your at it, what does
Code:
`printf("%d",14*3600);`
yield for you? Also, what about if you cast your result as a double before assigning it to a double?

6. I am sorry for the tags.

Code:
`printf("%d",14*3600);`
will this ever work? it will give negative value.

The small program works now, but the big one keeps saying the same crap i have never seen.

7. ...and how did you make the small program work? Also, unless your system uses 16 bit signed integers, then that printf statement should be positive. As for using longs, there really is no reason a signed long should give a negative value for that as far as I can see.

8. >Trust me, i know what i am doing.
Saying this and then showing us void main doesn't inspire confidence in your statement.

>will this ever work?
Probably not, unless you start giving us the information we ask for. What compiler and version of that compler are you using? What OS are you using? Can you deign to give us a complete program that exhibits the problem rather than snippets?

9. I just restarted the compiler.
yeah, i know this sounds incredibble, but it's the truth.
As for the big program, i have casted the result as a double, along with other variables, and it works too. Thanks
But i still don't understand. What is the difference between

Code:
`x=t.ti+hour * 3600`
(gives a negative value) and
Code:
`x = (double) t.ti_hour *  3600`
(this one works).

Thanks again for the help.

10. The compiler is Borland 3.1 and the OS is Windows Xp.

11. Perharps use correct code:
Code:
```#include <iostream.h>

int main(){
double x;
x=14*3600;
printf("%.0f",x);
cin.get();
}```
Iostream.h or stdio.h are necessary for the program to recognize the printf function
DO NOT USE VOID MAIN.
This program exits before you can see anything, that's why I added cin.get().

If you submit some code, make sure you don't make these MISTAKES again.
Btw, this program returned 50400.

But i still don't understand. What is the difference between

Code:
`x=t.ti+hour * 3600`
(gives a negative value) and
Code:
`x = (double) t.ti_hour *  3600`
(this one works).
The difference depends on what your compiler and OS is, as we've asked you for. I may have given you the trick for solving it, but we're no closer to knowing why it really happened. I'd suspect your compiler doesn't like casting returns from operators as it would cast just a regular integer. Maybe your compiler's multiplication operator doesn't even return an integer when multiplying integers, but something that isn't properly assigned to a double. Who knows... you can give up here and hope you never run into it again or you can figure out what went wrong. We need info from you, first.

13. DO NOT USE VOID MAIN WTF IS WRONG WITH YOU HAVENT YOU HEARD THIS BEFORE???
Well, maybe nothing is wrong with him, he just maybe didn't knew, you shouldn't push him around like this... But anyways, someone in the past gave me a link to some webpage containing informations about these kind of C++ "rules", though I can't find it. I think it used to be posted somewhere here... It could be usefull to qmadd, too...

14. Originally Posted by maxorator
Iostream.h or stdio.h are necessary for the program to recognize the printf function
On the topic of correct code, I didn't know the standard says iostream has to include stdio.h or define the printf() function, so I'm not sure your code, right there, is even as portable as the original void main code. I could be wrong, though.
The compiler is Borland 3.1 and the OS is Windows Xp.
I dispise Borland. They play by their own rules. Try a different compiler, perhaps Dev-C++ or Code::Blocks.

15. >yeah, i know this sounds incredibble, but it's the truth.
It doesn't sound incredible at all, and we believe you.

>What is the difference between
Borland 3.1 is a 16-bit compiler. The very fact that it works on Windows XP astounds me, but I can easily see all kinds of undefined behavior from your overflowing arithmetic. Casting the expression to double forces the range to increase drastically, and suddenly you're no longer overflowing your data types. Simple. Well, sort of.

>The compiler is Borland 3.1 and the OS is Windows Xp.
You really should update your compiler. The one you're using is ancient.