Results 1 to 7 of 7

Thread: Float error

  1. #1
    peliasrojo is offline Member
    Join Date
    Apr 2014
    Posts
    7
    Rep Power
    0

    Default Float error

    Hi. I've made a simple program in Java (Eclipse) that converts Farenheit to Celsius.

    The class is:

    Java Code:
    package ehu.student;
    
    public class ConversorTemperaturas {
    
    	/*--------------------------------------------------------------*/
    	/*Clase que dado una temperatura en farenheit la canvierte en șC*/
    	/*--------------------------------------------------------------*/
    	public float farenheit2celsius (float d){
    		float celsius, aux;
    		aux=d-32;
    		
    		celsius=aux* 5/9;
    		return celsius;
    	}
    
    }
    And the test code is:

    Java Code:
    package ehu.student;
    
    public class Prueba_temp {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		ConversorTemperaturas t = new ConversorTemperaturas();
    		System.out.println("Grados Farenheit: "+t.farenheit2celsius(25.6));
    		
    		
    	}
    
    }
    When I write t.farenheit2celsius(25.6), appears the following error:
    "The method farenheit2celsisus(float) in the type ConversorTemperaturas is not aplicable for the arguments (double)"
    If i change in the class the type float for double there is not problem. Why can't i use the type float?

    Thanks, regards peliasrojo.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,029
    Rep Power
    6

    Default Re: Float error

    You can, but by default Java makes a floating point literal a double type.

    Java Code:
    t.farenheit2celsius(25.6f));
    That's how you use a float. But I have a little tip for you: don't use float, use double. Java is also giving you that hint by defaulting to double. You simply have more precision and thus less chance of an overflow error when you use a double.

    In a "real" application I would even go further: don't use float or double, always use a BigDecimal.
    Last edited by gimbal2; 04-10-2014 at 10:05 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    peliasrojo is offline Member
    Join Date
    Apr 2014
    Posts
    7
    Rep Power
    0

    Default Re: Float error

    Thanks gimbal2. The exercise is with educational purpose and the teacher want the type float. I always work with double.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,029
    Rep Power
    6

    Default Re: Float error

    Ah yes, the blasted teachers. Indeed, do what they want and then when you're no longer bothered by them do it correctly.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Float error

    Quote Originally Posted by gimbal2 View Post
    In a "real" application I would even go further: don't use float or double, always use a BigDecimal.
    I think that is overkill; BigDecimals also 'suffer' from those 'ulp's (Unit of Least Precision); the only thing you can do to circumvent them is increase the number of decimals, but then those ulps show up again (only smaller) but you have increased the memory requirements and reduced the processing speed ... a.a.m.o.f. people are seldom interested in the exact value, they're only interested in the value they want to see (e.g. 0.499999942723 looks funny when they're expecting a half) and the formatters can be of help here ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,029
    Rep Power
    6

    Default Re: Float error

    Where I will accept a correction is that I was generalizing: bad Gimby. It depends on the type of application, indeed. For a game I would stick to doubles for example.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: Float error

    Quote Originally Posted by gimbal2 View Post
    Where I will accept a correction is that I was generalizing: bad Gimby. It depends on the type of application, indeed. For a game I would stick to doubles for example.
    True; there are lots of applications where floats/doubles are just fine, e.g. my Simplex and interior point algorithms would die a horrible death if I implemented them by using BigDecimals; there are very good other ways to keep numerical inaccuracy under control without having to use those slow, big, beasts ... some money related problems sometimes need them ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. [Q] - pi as a float
    By oran0007 in forum New To Java
    Replies: 1
    Last Post: 09-30-2011, 10:33 AM
  2. regarding int and float
    By lakshmibvaraprasad in forum New To Java
    Replies: 2
    Last Post: 07-24-2011, 08:30 PM
  3. Replies: 7
    Last Post: 04-26-2011, 03:27 PM
  4. Error in float value
    By JMaste in forum New To Java
    Replies: 5
    Last Post: 07-11-2010, 04:08 PM
  5. Error with convert to Float
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 02:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •