Ask your own question, for FREE!
Mathematics 22 Online
OpenStudy (anonymous):

c++ programming help

OpenStudy (anonymous):

so z is a float variable, it says that x = y * z is bad programming style but i don't know why.

OpenStudy (anonymous):

x = y * z; is the actual statement, also all the other variables are int

OpenStudy (anonymous):

Sorry brother, am not made for math...

OpenStudy (anonymous):

umm this isn't math

OpenStudy (anonymous):

the problem doesn't lie in the math

OpenStudy (anonymous):

i believe i might have to put the float variable before the int, like this x = z * y;

OpenStudy (anonymous):

Dude this is beyond me...

OpenStudy (anonymous):

otay

OpenStudy (dan815):

why dont you see the error it accumulates in c++

OpenStudy (anonymous):

it's doesn't have bad syntax just bad programming style

OpenStudy (anonymous):

atleast i think

OpenStudy (dan815):

add y*z to itself 10000 times and see the difference between z*y 10000 times

OpenStudy (anonymous):

ur such a troll

OpenStudy (dan815):

i dont know how C++ floating point arithmetic works, it might do int * float and float * int in different ways

OpenStudy (dan815):

why?

OpenStudy (anonymous):

cause reasons

OpenStudy (anonymous):

jk

OpenStudy (anonymous):

oh well ill just post what i think

OpenStudy (anonymous):

turn in*

OpenStudy (anonymous):

not post

OpenStudy (dan815):

you should google it im sure you can find these kinda questions there a lot

OpenStudy (ranga):

Mixing and matching int and float in a calculation is generally not considered a good practice. For one there will be truncation error in the case when int y = 2; float z = 3.2 then int x = y * z will be 6. But if you must mix them, use an explicit cast. Here is an example of mixing and matching int and float that gives different results: int p = 3; int q = 2; float x = 7.3; float result; result = x + p/q; (result will be 8.3 because p/q = 3/2 is an integer division that will be truncated to 1 which when added to 7.3 yields 8.3) result = x + (float) p / (float) q; (result will be 8.8)

OpenStudy (e.mccormick):

For C++, ask in comp sci section.

OpenStudy (anonymous):

kk

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!