Method's choose: Float and double Why ? pls!

void testDoit()
          float f = 3.14f;
          doIt(f);
     void doIt(Float f)
          System.out.println("Float");
     void doIt(double f)
          System.out.println("double");
why "double" is the ouput in the previous example!

hummm
thanks man!
Really, acording the "Java primitives data types"
a float (32 bits) is a double ( 64 bits ) !
mathematically
double contains float!
then, before a cast be executed...the second method is choosen
Thank you!
;)

Similar Messages

  • Float and Double - 5.1 divide by 3.0

    Hi everyone,
    I am quite new to java programming in term of using it. I wrote a test class to learn about float and double. However when I wrote the below code, I got a strange result. So, I wonder if java's float and double have any kind of exception I should be aware of. This won't happen if I just change from 3 to other numbers. Any suggestion? Thanks in advance.
    public class MathTest {
    public static void main (String args[]) {
    float f1 = 5.1F;
    float f2 = 3.0F;
    double d1 = 5.1;
    double d2 = 3.0;
    System.out.println("Float result : " + f1/f2);
    System.out.println("Double result : " + d1/d2);
    Result:
    Float result : 1.6999999
    Double result : 1.7

    This is due to the way that binary numbers convert to digital numbers and is expected. See these threads for explanations:
    http://onesearch.sun.com/search/onesearch/index.jsp?qt=%2Bprecision+%2Bwrong+double&col=developer-forums&rf=0&chooseCat=allJava&subCat=siteid%3Ajava

  • Confused with float and double

    Hi,
    I have done the following program which is supposed to deal with temperature and scales.
    import java.math.BigDecimal;
    public class Temperature {
         private float temp;
         private char scale;
         public static void main(String args[]) {
              Temperature t = new Temperature(40.5, 'F');
              System.out.println(t.getCTemperature());
         public Temperature() {
              temp = 0;
              scale = 'C';
         public Temperature(float pTemp) {
              temp = pTemp;
              scale = 'C';
         public Temperature(char pScale) {
              temp = 0;
              scale = 'C';
         public Temperature(float pTemp, char pScale) {
              temp = pTemp;
              scale = pScale;
         public float getCTemperature() {
              if (scale == 'C') {
                   return temp;
              } else {
                   float celciusTemp = 5 * (temp - 32) / 9;
                   BigDecimal bd = new BigDecimal(celciusTemp);
                   return bd.setScale(1, BigDecimal.ROUND_HALF_UP).floatValue();
    }When I try to compile I get the following error :
    F:\programs\ch4>javac Temperature.java
    Temperature.java:8: cannot find symbol
    symbol  : constructor Temperature(double,char)
    location: class Temperature
    Temperature t = new Temperature(40.5, 'F');
    *^*
    *1 error*
    I don't understand why the number 40.5 is considered like a double and that I can't use Temperature(float, char);
    If I'm not mistaken the compiler is asking for a constructor like Temperature(double, char) right ??
    thanks.

    Can I second Mr TuringPest, and Sir J.Bloch... Do NOT use floats, unless of course you have a really really good reason. Internally, java does all it's floating point arithmetic in doubles anyways, so you're just shooting yourself in the accuracy-foot by forcing it to: promote to double to calculate, "narrow" result back to float... promote, narrow ... promote, narrow ... promote, narrow ... The net result can be astoundingly inaccurate... So just use doubles.

  • Precision with float and double values

    Hi, does anyone knows how can i get more precise calculations with floating point numbers? Let's see an example:
    public class Teste {
    public static void main(String args[]) {
    float i = 0;
    while (i<=10) {
    System.out.println(i);
    i=i+0.1f;
    /* output
    0.0
    0.1
    0.2
    0.3
    0.4
    0.5
    0.6
    0.70000005
    0.8000001
    0.9000001
    1.0000001
    1.1000001
    1.2000002
    1.3000002
    1.4000002
    1.5000002
    1.6000003
    1.7000003
    1.8000003
    1.9000003
    2.0000002
    2.1000001
    2.2
    2.3
    2.3999999
    2.4999998
    2.5999997
    2.6999996
    2.7999995
    2.8999994
    2.9999993
    3.0999992
    3.199999
    3.299999
    3.399999
    3.4999988
    3.5999987
    3.6999986
    3.7999985
    3.8999984
    3.9999983
    4.0999985
    4.1999984
    4.2999983
    4.399998
    4.499998
    4.599998
    4.699998
    4.799998
    4.8999977
    4.9999976
    5.0999975
    5.1999974
    5.2999973
    5.399997
    5.499997
    5.599997
    5.699997
    5.799997
    5.8999968
    5.9999967
    6.0999966
    6.1999965
    6.2999964
    6.3999963
    6.499996
    6.599996
    6.699996
    6.799996
    6.899996
    6.9999957
    7.0999956
    7.1999955
    7.2999954
    7.3999953
    7.499995
    7.599995
    7.699995
    7.799995
    7.899995
    7.9999948
    8.099995
    8.199995
    8.299995
    8.399996
    8.499996
    8.599997
    8.699997
    8.799997
    8.899998
    8.999998
    9.099998
    9.199999
    9.299999
    9.4
    9.5
    9.6
    9.700001
    9.800001
    9.900002
    */

    http://forum.java.sun.com/thread.jsp?forum=31&thread=357174

  • What is class method for a float

    What is a class method for a float, and can I use it with a timer?

    X- Developer wrote:
    What is a class method for a float, ...
    A float is a simple data type that just stores one real number. Other simple data types are, for example, char, int, long, double, etc. None of these is a class. None have member variables, instance methods or class methods.
    The methods of some classes certainly use floats, and some methods have "float" in their name. For example, \[NSString floatValue\] will convert numeric text into a float value. Let us know if your question relates to a method like that. If so, remember to always specify which class you're asking about.
    can I use it with a timer?
    Any method can set up a timer which fires some other method once or repeatedly. However I don't understand what it means to use a float with a timer. In fact I don't know what it might mean if an int or a string or a view were used with a timer. Maybe it would help if you described what you're trying to do, ok?
    \- Ray

  • What is the match of float or double data type in abap?

    I have tried p, but I could not handle decimal numbers such as 10.6 or 10,6(no matter which one is preferred, the question is the same...).
    How can I handle these numbers?
    Thanks.

    Hi
    Hope you know about FLOAT and Double.But you can use the same P for handling decimal places...
    Use like this.
    Data :Deci type P DECIMALS 2.
    Get bckif you are getting error still...
    Reward All Helpfull answers..........

  • How to format floats (or doubles ) but not converting to String?

    Hi everybody,
    my question is simple:
    I have a number,lets say 51,4567321 and i want to format it ,as a float or double ,as 51,45.
    One way is to do:
    float x=((hits * (float) 100) / this.getAccesses());
    DecimalFormat perCentFormatter = new DecimalFormat("00.00");
    String formattedNum=this.perCentFormatter.format(x);and that will create a string "51,45".
    But how do i do to create a float or double or anything else that holds the value with 2 decimal points after mantissa?e.g XX,XX
    Thank you very much,
    Chris

    A few options:
    1) just live with it. The float and double formats hold their values in binary, you know, and you only have approximations of multiples of tenth and one-hundreds of unity. It's all just approximations for those types. C'est la vie.
    2) Use java.math.BigDecimal, which gives you control over rounding.
    3) Change your design, so you use integers with the lower unit. So if the float currently "51,23" represents 51 Euros and 23 cents (what's the word? Europennies?), then you'd use an int of 5123 to represent five thousand one hundred and twenty-three cents.

  • Double - Float casting or Why isn't there float sqrt?

    Is there any reason why not to include float sqrt? I'm pretty pissed since, it is just... well plain stupid. I hope it can be added, not to mention that casting takes away performance, not much, but I have to do a lot of casting since I'm doing 3d grahpics... Anyone know any solluions? Is there any point going the hard way and implementing own sqrt for float?
    WHY WHY WHY, OH WHY.
    Something is rotten in the state of java.

    The absence of Math.sqrt(float) and float versions of other relevant methods is apparently becuase early versions of the Java compiler could not distinguish overloaded methods which only differed in double/float parameters. The current compilers do not have this problem and it has been proposed that the extra methods should be added but I don't know when this might happen.
    In any case how sure are you that casting between double and float is causing any significant performance problem? This cast is NOT particularly expensive (casting to 'int' from float or double is usually much more expensive).
    What processor are you using --- as far as I know Intel processors do not give any speed benefit from doing sqrt on float instead of double. I believe that many RISC processors do benefit from using float where possible.

  • When I tried to open my Mail app this morning, it opened a list of choices - iCloud, E Exchange, Google, Yahoo, Aol, Outlook.  I use Hotmail and it usually opens to my Inbox.  Why did it do this and do I choose Outlook and is it safe to sign in

    When I tried to open my Mail app on my IPhone this morning, it opened a list of choices - iCloud, E Exchange, Google, Yahoo, Aol, Outlook.  I use Hotmail and it usually opens to my Inbox.  Why did it do this and do I choose Outlook and is it safe to sign in? 

    It sounds like instead of opening your Mail App, you actually opened your Settings App, and were somehow in the 'Add Account' section of Mail, Contacts & Calendars.
    Close the apps, and then reopen just the Mail App.

  • When I close Firefox I choose Save and Quit to save my tabs for next opening. It's not saving them the last few times, wondering why?

    Upon closing Firefox it asks me if I'd like to save and quit, quit, or cancel. I always choose save and quit. I've noticed the last few times that my tabs were not saved the next time I opened Firefox, I'm wondering why. I've lost some webpages I was saving for later now, because of this.

    The computer crashes, due to power failure, with Firefox running. After rebooting, Firefox has lost record of tabs. [History/Recently Closed Windows] is grayed out. So the list of tabs is lost. It should be possible for the software to keep a periodic backup of current tabs, such as, e.g., MS Word makes backups of the current file every 10 minutes, so that the tabs can be restored if the software does not make a normal exit due to a computer crash.

  • When I type I cannot add text when I select the text area and double click. Why?

    When I type I cannot add text when I select the text area and double click. Why?

    Premiere Elements 11  trial version for macMac computer photos uploaded to the timeline just finetrying to make a new title, streaming or in a framedouble clicked on the text in the box and nothing shows up when I type on my keyboard. I can delete the words that are there but not type in new ones. I selected the type and size, etc.
    Thanks if you can figure it out. I gave up!
    I may just un-install it all together if I can't get to work.Joanne
    Date: Thu, 9 May 2013 17:59:08 -0700
    From: [email protected]
    To: [email protected]
    Subject: When I type I cannot add text when I select the text area and double click. Why?
        Re: When I type I cannot add text when I select the text area and double click. Why?
        created by A.T. Romano in Premiere Elements - View the full discussion
    Joanne Murray J2 What version of Premiere Elements are you using and on what operating system (including 32 or 64 bit) is your Premiere Elements installed? Forgive the questions if you have been there and done that, but I do not want to take anything for granted that might hinder a speedy resolution to your issue. Are you opening an already created title that is sitting on the Timeline in order to edit it in the Titler or are you trying to create a new Title (Default Text or other)? Do you know how to work with the Selection Tool and Type Tool in the Titler? Once I know what version of Premiere Elements that you are using, I will give you specific how to details. Thanks. ATR
         Please note that the Adobe Forums do not accept email attachments. If you want to embed a screen image in your message please visit the thread in the forum to embed the image at http://forums.adobe.com/message/5307494#5307494
         Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page: http://forums.adobe.com/message/5307494#5307494
         To unsubscribe from this thread, please visit the message page at http://forums.adobe.com/message/5307494#5307494. In the Actions box on the right, click the Stop Email Notifications link.
         Start a new discussion in Premiere Elements by email or at Adobe Community
      For more information about maintaining your forum email notifications please go to http://forums.adobe.com/message/2936746#2936746.

  • Every time i double clicked the group to do some editting, it crashed and stopped working. win8 and cs6. why!!

    every time i double clicked the group to do some editting, it crashed and stopped working. win8 and cs6. why!!

    What happens if you turn off the preference double click to isolate.

  • Dimensions - int and double mixed?

    Dimension x = new Dimension(int_1, int_2);
    int height = x.height;
    int width  = x.width;
    double height_d = x.getHeight();
    double width_d = x.getWidth();When you create a dimension you give it to integers as the width and height. However, if you call getHeight() or getWidth() it returns double-precision values. What's the point? Why does it use int/doubles?
    Thanks

    The methods getWidth/getHeight are inherited from RectangularShape.
    We have:
    Rectangle2D extends RectangularShape
    Rectagle extends Rectangle2D //implemeted with ints
    Rectangle2D .Float extends Rectangle2D //implemeted with floats
    Rectangle2D .Double extends Rectangle2D //implemeted with double
    So the implementations are providing the precision. Since the getWidth/getHeight methods are common
    to the hierarchy they return doubles to avoid loss of precision in the Rectangle2D .Double case.
    On the other hand, if I'm working with a java.awt.Rectangle I just access the public width and height fields
    directly.

  • Error when calling method with a return of double in j2me

    hello all,
    i have following problem with a j2me program:
    if i call a method with a return of double, then i get following error
    message:
    ERROR: floating-point constants should not appear
    Error preverifying class test.hallo
    what i'm doing wrong
    thanks in regard
    ----------------example----------------------------
    double yourValue(int y, int m, int d)
    double v = 0.10;
    v = 3.39 y m *d
    return v;
    public void startApp()
    int td;
    int y =2;
    int m =2;
    int d =2;
    td = yourValue(y,m,d);
    return(td);

    It's true for MIDP 1.0.
    But you can always use implementation of the float
    point arithmetic which was written by independent
    developers. For example see J2ME section of my
    homepage http://henson.newmail.ru
    anyway, double is reserved word in java, the way you wrote the source code in your example neither the preverifier nor the compiler will recognize that you intend to use your own types for double and float. maybe with Double or Float it would be different ...
    further question: you declare a void function, in the body, however, you try to return some value. something wrong with this function??
    regards
    bernard

  • Error:  java/lang/String  & parsefloat not a method in class Float (j0235)

    Hi,
    I don't know why i amgetting this error which says: 'unable to locate system class: java/lang/String.
    I have compiled and executed the reader method separately on a command prompt and it is working, but when i do it in microsoft visual j++, i am getting this run time error.
    I also have rt.jar in the classpath.
    If i don't have this rt.jar in my classpath, it is giving me a compilation error: parsefloat is not a method in class Float (j0235).
    somebody please help.
    Jagadish.
    // an agent is floating randomly.
    import java.util.*;
    import vrml.*;
    import vrml.node.*;
    import vrml.field.*;
    import java.io.*;
    public class FloatingAgent extends Script{
    SFVec3f setAgentPosition;
    SFRotation setAgentRosition;
    static int count=0;
    float agentPosition[] = new float[3];
    float agentRosition[] = new float[4];
    float rotangle = 0.0f;
    float aRad= (float) (Math.PI/180);
    public void initialize(){
    setAgentPosition =
    (SFVec3f)getEventOut("setAgentPosition");
    setAgentRosition =
    (SFRotation)getEventOut("setAgentRosition");
    // initialize the agent position.
    agentPosition[0] = 0.0f;
    agentPosition[1] = 0.0f;
    agentPosition[2] = 0.0f;
    agentRosition[0] = 0.0f;
    agentRosition[1] = 0.0f;
    agentRosition[2] = 1.0f;
    agentRosition[3] = 0.0f;
    public void processEvent(Event e){
    if(e.getName().equals("interval") == true){
    moveAgent();
    void moveAgent()
    agentPosition = reader();
    rotangle += 2.0f;
    agentRosition[3] = rotangle * aRad;
    // move the agent to the new position.
    setAgentPosition.setValue(agentPosition);
    setAgentRosition.setValue(agentRosition);
    }//move agent
    static float[] reader()
    float p1[] = new float[3];
    try{
    FileReader fr = new FileReader("data.txt");
    BufferedReader br = new BufferedReader(fr);
    String s;
    int count1=0;
    count++;
    try{
    while((s=br.readLine())!=null)
    count1++;
    StringTokenizer st = new StringTokenizer(s);
    if(count1==count)
    int i=0;
    while(st.hasMoreTokens())
    p1[i++]=Float.parseFloat(st.nextToken());
    }//if
    }//end of stringTokenizer while.
    fr.close();
    catch(IOException f)
    System.out.println("file cannot be opened");
    }//try
    catch(FileNotFoundException e)
    System.out.println("file doesn't exist");
    } //try
    return p1;
    }//reader

    still using Visual J++ ???

Maybe you are looking for