• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

float * double...newbie question

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I'm all tasted up for a BLT! This tiny ad wants a monte cristo!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic