Double to float conversion

edited January 2014 in Programming Questions

Hello, I'm new to Processing and coding in general. Could someone explain to me why double is converted to float the way it is in the following statement,

double c = 1.473974; float f = (float) c;

instead of using the processing type cast functions? i.e.

float f = float(c);

What's the difference in the two methods? Thanks.

Tagged:

Answers

  • edited January 2014 Answer ✓

    Function float() belongs to Processing's bundled API.
    While (float) is a Java's native cast operator!

    A cast operator would always be faster than a function call.
    While a function call would have more features, like converting a whole array!

  • From the API: http://www.processing.org/reference/floatconvert_.html

    "Converts an int, string, or array to its floating point representation."

    So, like GoToLoop said, the float() function can convert Strings or arrays as well as ints (or doubles). The explicit (float) cast can only convert to float from other number types like int or double.

  • But why the error when I try to convert double to float using Processing's type cast function?

    "The method parseFloat(int) in the type Papplet is not applicable for the arguments (double)"

  • Answer ✓

    Because parseFloat() can't convert from double to float, use either float(d) or (float)d, like mentioned from GoToLoop and KevinWorkman.

  • No actually, float(d) doesn't work but (float)d works. I just wanted to know why the former doesn't and gives the aforementioned error.

  • Poersch already answered that question. The float() method can only take an int, not a double. I was wrong when I said it could take a double. It can only take an int, a String, or an array.

  • oh..ok. thanks!

  • edited January 2014 Answer ✓

    It can only take an int, a String, or an array.

    Just for the sake of completeness, outta the 8 Java primitives, these following half 4 don't work for float():

    • boolean
    • long
    • float (Yup! float() doesn't accept its own type! :O) )
    • double

    Now these 4 half do:

    • byte
    • short
    • char
    • int

    Anyways, for primitive conversions, a native cast operator is guaranteed to be faster! $-)
    Leave function float() for more complex conversions like array & String.

  • I think that byte, short, and char work because there's an implicit cast to int when you pass them into the float() function.

  • edited January 2014

    ... there's an implicit cast to int.

    I believe that it is so too. B/c byte, short & double are completely neglected by Processing devs! I-)

    However, conversion function int() is much more flexible! Amongst the 8 primitive data-types, only double isn't accepted.
    Yup, it means we can even convert boolean to int using int()!!! \m/

    And researching more of it, found out that str() converts the same data-types as int().
    While char() has the same restrictions as float()!

    In the end, all of them share the common hatred towards double!!! [..]

  • Yep. From the API: http://www.processing.org/reference/float.html

    "Processing supports the 'double' datatype from Java as well, however it is not documented because none of the Processing functions use double values, which are overkill for nearly all work created in Processing and use more memory. We do not plan to support doubles, as it would require increasing the number of API functions significantly."

  • edited January 2014

    We do not plan to support doubles, as it would require increasing the number of API functions significantly.

    I agree as well. But not for the converter functions. They should be a reliable bridge by definition anyways! [-X

Sign In or Register to comment.