What will be the result of this:
int y = 1;
float x = .7;
int y = x * y;
Will y be converted to a float and contain .7, or stay an integer and be 1?
What about if x = .4, and y stays an integer? will it contain 0?
thanks
What will be the result of this:
int y = 1;
float x = .7;
int y = x * y;
Will y be converted to a float and contain .7, or stay an integer and be 1?
What about if x = .4, and y stays an integer? will it contain 0?
thanks
The result will be an error because you declared variable y twice. What did I win?
The nice thing about Arduino is that in less time it took you to post that, you could have typed in and uploaded a program to see the answer.
Well, I can see right new, this being my first time on this site, people are not very helpful!
I obviously meant int z = x * y, and my Arduino mini metro is buried inside my underwater ROV, of which I don't desire to remove the end caps tonite to access the USB port.
Thanks for the help.
p.s. in the time it took for the wise ass repliess you guys could have answered my question
aarg was spot on. We don't like to guess, because it could have been many things.
jremington was also very right. Arduino is about fast prototyping and learning along the way.
We still can't answer your question. Is 'z' an integer or a float ? If it is an integer, it will always contain an integer and never a float value, therefor 0.7 becomes 0. If is is a float, multiplying an integer with a float is done with float math, so the result is accurate.
If you want to mix float and int, you have to be sure that it will be okay. Can you show us the code ?
We're not mind readers. What you give us, is what we have to work with. Would it be helpful to guess what you mean, answer and give you a completely wrong answer?
Also, in future, if you want to test basic C/C++ constructs, you don't need an Arduino to do it. You can easily obtain a compiler to run on your PC.
Actually, the IDE will compile without an Arduino attached. It's the little check mark at the upper left that says, "verify" when you hover over it.
my Arduino mini metro is buried inside my underwater ROV
I should have known that! I apologize profusely.
p.s. in the time it took for the wise ass replies you guys could have answered my question
A couple of things:
First, buy a few extra Nanos for $3 so you can run tests if your ROV is at the bottom of the lake.
Second, take a look at the number of posts and karma points of the guys who took the time to answer your post. I know they have helped thousands of people who have demonstrated that they have tried everything in their power to answer the question themselves. When they see a question like yours, they get a tad miffed because you didn't take the time to answer it yourself, but sought the easy way to the answer by asking someone else to do your work for you.
Third: Google is your friend. If you can't access your hardware, search the web. I typed in "arduino mixed data type resolution" and got 285,000 hits. Probably one of those would have answered your question.
You'll eventually discover that this is the best place on the planet to get those really tough Arduino questions answered. You'll also discover that people here have short shrift with those who expect us to do all the work.
model14:
I obviously meant ....
There's no "obviously" about it.
I was curious too:
void setup()
{
Serial.begin(9600);
}
void loop()
{
int y = 1;
float x = 0.7;
int IntyZ = x * y;
float FloatyZ = x * y;
Serial.print("IntyZ="); Serial.println(IntyZ);
Serial.print("FloatyZ = "); Serial.println(FloatyZ);
delay(1000);
}
I put it in the loop, just in case the answer changes over time But consistently I get:
IntyZ=0
FloatyZ = 0.70
Just for fun, I changed IntyZ to:
int IntyZ = x * y + 0.5;
Now I get IntyZ = 1.
So I was curious about whether the truncation to zero was happening before or after the multiplication. According to these guys it would be after:
So really you have this: IntyZ = (int)((float)x * y);
Disassembling the (modified) program demonstrates:
int DoMult(int x, float y)
{
1bc: cf 93 push r28
1be: df 93 push r29
1c0: 00 d0 rcall .+0 ; 0x1c2 <_Z6DoMultif+0x6>
1c2: 00 d0 rcall .+0 ; 0x1c4 <_Z6DoMultif+0x8>
1c4: cd b7 in r28, 0x3d ; 61
1c6: de b7 in r29, 0x3e ; 62
1c8: 9a 01 movw r18, r20
1ca: ab 01 movw r20, r22
return x * y;
1cc: bc 01 movw r22, r24
1ce: 88 27 eor r24, r24
1d0: 77 fd sbrc r23, 7
1d2: 80 95 com r24
1d4: 98 2f mov r25, r24
1d6: 29 83 std Y+1, r18 ; 0x01
1d8: 3a 83 std Y+2, r19 ; 0x02
1da: 4b 83 std Y+3, r20 ; 0x03
1dc: 5c 83 std Y+4, r21 ; 0x04
1de: 0e 94 f8 06 call 0xdf0 ; 0xdf0 <__floatsisf>
1e2: 29 81 ldd r18, Y+1 ; 0x01
1e4: 3a 81 ldd r19, Y+2 ; 0x02
1e6: 4b 81 ldd r20, Y+3 ; 0x03
1e8: 5c 81 ldd r21, Y+4 ; 0x04
1ea: 0e 94 ac 07 call 0xf58 ; 0xf58 <__mulsf3>
1ee: 0e 94 c5 06 call 0xd8a ; 0xd8a <__fixsfsi>
The line in red is the call to a floating point library (the Arduino can't do this by itself).
Which is going to use up a bit of program memory.
Interesting question!