• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

float * double...newbie question

 
David Llewellyn
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Llewellyn
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic