aspose file tools*
The moose likes Beginning Java and the fly likes float * double...newbie question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "float * double...newbie question" Watch "float * double...newbie question" New topic
Author

float * double...newbie question

David Llewellyn
Ranch Hand

Joined: Feb 01, 2005
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

Joined: Dec 29, 2004
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.

Instead of

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.


[Jess in Action][AskingGoodQuestions]
David Llewellyn
Ranch Hand

Joined: Feb 01, 2005
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
 
jQuery in Action, 2nd edition
 
subject: float * double...newbie question