Page 1 of 1
Float και Double (Java)
Posted: Sat Oct 23, 2010 3:42 pm
by MpoMp
Πείτε με χαζό, πείτε με περίεργο, αλλά ακόμα δεν μπορώ να καταλάβω ποια ακριβώς η διαφορά float και double και γιατί στην float πρέπει στο τέλος της τιμής της να βάζουμε το f.
Επίσης γιατί να βάλουμε f στο τέλος της τιμής μιας double? Και διάβασα κάπου στα tutorial της oracle πως μπορούμε να βάλουμε και ένα d στο τέλος της τιμής μιας float. Γιατί?
- Spoiler: εμφάνιση/απόκρυψη
Ρώτησα και στο φροντιστήριο αλλά και πάλι δεν έχουν ξεκαθαρίσει οι έννοιες στο κεφάλι μου.
Re: Float και Double (Java)
Posted: Sat Oct 23, 2010 4:42 pm
by ~~Wind~~
1. Ακρίβεια
2. Μέγεθος στη μνήμη
3. Πεδίο τιμών
από float σε double τα παραπάνω μεγαλώνουν
Re: Float και Double (Java)
Posted: Sat Oct 23, 2010 7:39 pm
by stoupeace
Αυτό που σε νοιάζει είναι ότι με τον float έχεις ακρίβεια νομίζω 7 δεκαδικά ψηφία, ενώ με τον double 16.
Δηλαδη oι floats: 0.000000611111 και 0.0000006232324 είναι ίδιοι για το PC, αν μετρησα καλα τα μηδενικά
Ο double προκειμενου να ειναι περισσοτερο ακριβης πιάνει περισσότερη μνήμη.
Re: Float και Double (Java)
Posted: Sat Oct 23, 2010 8:00 pm
by MpoMp
Γιατί στην float πρέπει στο τέλος της τιμής της να βάζουμε το f?
Επίσης γιατί να βάλουμε f στο τέλος της τιμής μιας double? Και διάβασα κάπου στα tutorial της oracle πως μπορούμε να βάλουμε και ένα d στο τέλος της τιμής μιας float. Γιατί?
Re: Float και Double (Java)
Posted: Sat Oct 23, 2010 8:07 pm
by stoupeace
MpoMp wrote:Γιατί στην float πρέπει στο τέλος της τιμής της να βάζουμε το f?
Επίσης γιατί να βάλουμε f στο τέλος της τιμής μιας double? Και διάβασα κάπου στα tutorial της oracle πως μπορούμε να βάλουμε και ένα d στο τέλος της τιμής μιας float. Γιατί?
Το 1ο γιατι έτσι.
Το 2ο γιατι μπορεί να'χεις δηλώσει κάτι double και να θες τη συγκεκριμένη φορά να υπολογιστεί float (νομίζω, δε θυμάμαι κιόλας).
To 3o δεν ξερω τι βγαζει η oracle απο το μυαλό της
# float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in section 4.2.3 of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.
# double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in section 4.2.3 of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.
http://download.oracle.com/javase/tutor ... types.html
Re: Float και Double (Java)
Posted: Sat Oct 23, 2010 10:32 pm
by maxthebest
Απλο ειναι :
1.0123f ειναι float.
1.0123d ειναι double.
1.0123 ειναι double λογο οτι επελεξε αυτο η sun ως default (γιατι εχει καλυτερη ακριβια αρα ειναι πιο καλο).
θα προτιμουσες να σου εριχνε "Illegal Constant" o compiler για τυπολατρικους λογους ?
Re: Float και Double (Java)
Posted: Sun Oct 24, 2010 3:55 pm
by MpoMp
stoupeace wrote:
To 3o δεν ξερω τι βγαζει η oracle απο το μυαλό της
# float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in section 4.2.3 of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.
# double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in section 4.2.3 of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.
http://download.oracle.com/javase/tutor ... types.html
ΟΚ, άρα απλά έτσι φτιάξανε τη γλώσσα, να βάζεις και ένα f μετά τις float. (Όμως δεν γίνεται να το κάνανε χωρίς λόγο... Τέλος πάντων.)
The floating point types (float and double) can also be expressed using E or e (for scientific notation), F or f (32-bit float literal) and D or d (64-bit double literal; this is the default and by convention is omitted).
double d1 = 123.4;
double d2 = 1.234e2; // same value as d1, but in scientific notation
float f1 = 123.4f;
Για το 3ο, όπως φαίνεται απλά παραλείπεται.
Re: Float και Double (Java)
Posted: Thu Nov 04, 2010 12:16 pm
by Zifnab
Χωρίς να είμαι σίγουρος:
float f1= 5/2 => 2.00
float f2= 5/2.0=> 2.5
float f3= 5/2f => 2.5
float f4= 5f/2 => 2.5
φαντάζομαι γράφοντας έναν αριθμό τεράστιο(που δεν χωράει σε float) με ένα f δίπλα μπορείς να τον βάλεις σε float.
Δεν νομίζω να γίνεται float f = αριθμόςd ίσως με κάποιο warning για possible loss of imformation ... αλλά σίγουρα γίνεται double d = αριθμόςf.