Where to find a good link for dealing with math. Everything I find doesn't give any good answers I'm looking for. They always start and finish with the same types. All the numbers I will be using will be whole numbers so in trying to save space in the processor I would like to know which sample would be best or if there's a better way of doing it. If there a good link covering this topic I would appreciate have it posted.
Most important to know is that the data type (outcome) of an (partial) expression is the largest data type of that expression.
So your sample 1 multiplies an int and a long GIVES a long as answer, and then that long is assigned to a double.
This behavior can cause the side effect of under- and overflow. To prevent this you must select a data type that is large enough to hold the MIN/MAX value.
ANother technique is casting which works (simplified) as follows
If I'm right, overflow of datatype only if the Sum_Of_Numbers is grater then 4,294,836,225
Sample 4 where I used unsigned long as the data type
First_Number fill never be greater then 65,535 Second_Number fill never be greater then 65,535 so the Sum_Of_Numbers will never grater then 4,294,836,225
a unsigned long Ranges from 0 to 4,294,967,295
Thanks for input. I confused myself even more now so I'll do some more digging if I can remember what I really wanted to learn.
Even you have confused others more than you have confused yourself.
Your declaration:
double Product_Of_Numbers = 0;
says that you have declared a variable which will hold floating point number in binary64 format (supported by DUE); but, you have defined it by 0 rather than by 0.0.
GolamMostafa:
says that you have declared a variable which will hold floating point number in binary64 format (supported by DUE); but, you have defined it by 0 rather than by 0.0.
But, amazingly, the IEEE-754 floating point standard stores the value zero as all zeroes.
Cute, huh?
But Zero (0) is a real number -- when we go from + 3 to -3, we go through (across) it! If you ask me to present 27 in float form, I will present it as 27.00. Similarly, I will present 0 as 0.00!
GolamMostafa:
But Zero (0) is a real number -- when we go from + 3 to -3, we go through (across) it! If you ask me to present 27 in float form, I will present it as 27.00. Similarly, I will present 0 as 0.00!
The compiler is MORE than smart enough to know what when you set a float or double to 0, you mean 0.0. When you set it to 27, you mean 27.0. What else could it possibly assume? 0 should mean 0.0000001??? 0.5? Read up on HOW the compiler interprets literals, rather than making (incorrect) assumptions.
Regards,
Ray L.
RayLivingston:
The compiler is MORE than smart enough to know what when you set a float or double to 0, you mean 0.0. When you set it to 27, you mean 27.0. What else could it possibly assume? 0 should mean 0.0000001??? 0.5? Read up on HOW the compiler interprets literals, rather than making (incorrect) assumptions.
Regards,
Ray L.
The IEE754 spec has a certain range of integer numbers it can represent exactly. If you use a float as an index in a for loop small deltas in the last significant digit can add up (or sub down or both) resulting in just an extra iteration or in one less. Similar type of problem occurs when you compare two floats for equality, although they might print the same value they are not necessary equal.
Since "first_number" and "second_number" are both of type "unsigned int", the calculation on the right side of the equals sign is also done in "unsigned int"-sized registers (16bits on AVR), this overflows and results in an incorrect answer BEFORE the code gets around to assigning the result to the "double."
You have to give the compiler instructions to change at least one of the values on the right to a big-enough type BEFORE doing the calculation: