This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# float * double...newbie question

David Llewellyn
Ranch Hand
Posts: 45
I'm trying to multiply a value in a float array by a double...
this is the code I've got, but I'm getting a compiler error:

this is the error I'm getting:
FileReader2.java [142:1] operator * cannot be applied to float[][],double
returnPoint[0] = bezCurve[0][0] * vvv + bezCurve[1][0] * vvu + bezCurve[2][0] * vuu + bezCurve[3][0] * uuu;

It's probably simple, I'm quite a newbie though
cheers

Steven Bell
Ranch Hand
Posts: 1071
First off you're not going to be able to put the result of the multiplication into returnPoint[0] because it is a float and vvv, vvu, ect. are doubles.

Show the declaration of the bezCurve variable.

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24208
35
The result of multiplying (or adding, subtracting, or dividing) a float with a double is a double, and Java won't store a double into a float without a "cast". A cast is just a hint to the compiler that it's OK to convert the type of a value.

aFloat = aFloat * aDouble;

you have to write

aFloat = (float) (aFloat * aDouble);

Now, I should mention that in casting, there are potential problems; in particular, doubles can hold values that floats can't hold, and the cast can give you a dramatically different value (the too-large part will be simply truncated, giving you a different number all together.) Be careful, and consider simply using the same type everywhere.

David Llewellyn
Ranch Hand
Posts: 45
Thankyou to both of you. You were correct. Firstly the bezier array was a method parameter that I had declared incorrectly, and secondly I decided it was best to make everything double.

Thankyou very much for the speedy responses