Float και Double (Java)

Συζητήσεις για γλώσσες προγραμματισμού και θέματα σχετικά με προγραμματισμό.
Post Reply
User avatar
MpoMp
Venus Former Team Member
Posts: 6620
Joined: Mon Oct 04, 2010 9:31 pm
Gender:
Contact:

Float και Double (Java)

Post by MpoMp » Sat Oct 23, 2010 3:42 pm

Πείτε με χαζό, πείτε με περίεργο, αλλά ακόμα δεν μπορώ να καταλάβω ποια ακριβώς η διαφορά float και double και γιατί στην float πρέπει στο τέλος της τιμής της να βάζουμε το f.

Επίσης γιατί να βάλουμε f στο τέλος της τιμής μιας double? Και διάβασα κάπου στα tutorial της oracle πως μπορούμε να βάλουμε και ένα d στο τέλος της τιμής μιας float. Γιατί?
Spoiler: εμφάνιση/απόκρυψη
Ρώτησα και στο φροντιστήριο αλλά και πάλι δεν έχουν ξεκαθαρίσει οι έννοιες στο κεφάλι μου.
Any fool can make something complicated. It takes a genius to make it simple.
MpoMp @ Discord
User avatar
~~Wind~~
Gbyte level
Gbyte level
Posts: 1747
Joined: Thu Nov 30, 2006 1:07 am
Academic status: N>4
Gender:
Location: *void

Re: Float και Double (Java)

Post by ~~Wind~~ » Sat Oct 23, 2010 4:42 pm

1. Ακρίβεια
2. Μέγεθος στη μνήμη
3. Πεδίο τιμών

από float σε double τα παραπάνω μεγαλώνουν
ImageImage
Come to foss-aueb ...we have cookies.. yamyam~nomnomnomnomnomnomnomnom
a cookie ~ I will do science to it
User avatar
stoupeace
Wow! Terabyte level
Wow! Terabyte level
Posts: 5372
Joined: Tue Aug 26, 2008 4:08 pm
Academic status: High school
Gender:

Re: Float και Double (Java)

Post by stoupeace » Sat Oct 23, 2010 7:39 pm

Αυτό που σε νοιάζει είναι ότι με τον float έχεις ακρίβεια νομίζω 7 δεκαδικά ψηφία, ενώ με τον double 16.

Δηλαδη oι floats: 0.000000611111 και 0.0000006232324 είναι ίδιοι για το PC, αν μετρησα καλα τα μηδενικά :-p

Ο double προκειμενου να ειναι περισσοτερο ακριβης πιάνει περισσότερη μνήμη.
Η καλύτερη μπάντα όλου του κόσμου: Sonata Antartika
Mpomp is building an army army. And I got my head back.
░░░░░███████ ]▄▄▄▄▄▄▄▄
▂▄▅█████████▅▄▃▂ ____☻/︻╦╤─
Il███████████████████]. /▌
_◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙◤.. . / \
User avatar
MpoMp
Venus Former Team Member
Posts: 6620
Joined: Mon Oct 04, 2010 9:31 pm
Gender:
Contact:

Re: Float και Double (Java)

Post by MpoMp » Sat Oct 23, 2010 8:00 pm

Γιατί στην float πρέπει στο τέλος της τιμής της να βάζουμε το f?

Επίσης γιατί να βάλουμε f στο τέλος της τιμής μιας double? Και διάβασα κάπου στα tutorial της oracle πως μπορούμε να βάλουμε και ένα d στο τέλος της τιμής μιας float. Γιατί?
Any fool can make something complicated. It takes a genius to make it simple.
MpoMp @ Discord
User avatar
stoupeace
Wow! Terabyte level
Wow! Terabyte level
Posts: 5372
Joined: Tue Aug 26, 2008 4:08 pm
Academic status: High school
Gender:

Re: Float και Double (Java)

Post by stoupeace » Sat Oct 23, 2010 8:07 pm

MpoMp wrote:Γιατί στην float πρέπει στο τέλος της τιμής της να βάζουμε το f?

Επίσης γιατί να βάλουμε f στο τέλος της τιμής μιας double? Και διάβασα κάπου στα tutorial της oracle πως μπορούμε να βάλουμε και ένα d στο τέλος της τιμής μιας float. Γιατί?
Το 1ο γιατι έτσι.

Το 2ο γιατι μπορεί να'χεις δηλώσει κάτι double και να θες τη συγκεκριμένη φορά να υπολογιστεί float (νομίζω, δε θυμάμαι κιόλας).

To 3o δεν ξερω τι βγαζει η oracle απο το μυαλό της :-p
# 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
Η καλύτερη μπάντα όλου του κόσμου: Sonata Antartika
Mpomp is building an army army. And I got my head back.
░░░░░███████ ]▄▄▄▄▄▄▄▄
▂▄▅█████████▅▄▃▂ ____☻/︻╦╤─
Il███████████████████]. /▌
_◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙◤.. . / \
User avatar
maxthebest
Kilobyte level
Kilobyte level
Posts: 180
Joined: Fri Dec 02, 2005 5:23 pm
Location: Παράδεισος

Re: Float και Double (Java)

Post by maxthebest » Sat Oct 23, 2010 10:32 pm

Απλο ειναι :

1.0123f ειναι float.
1.0123d ειναι double.

1.0123 ειναι double λογο οτι επελεξε αυτο η sun ως default (γιατι εχει καλυτερη ακριβια αρα ειναι πιο καλο).
θα προτιμουσες να σου εριχνε "Illegal Constant" o compiler για τυπολατρικους λογους ?
Ούτοι συνέχθειν, αλλά συμφιλείν έφυν (Αντιγόνη στοίχος 523) =
Δεν γεννήθηκα για να συμμερίζομαι το μίσος , αλλά για να αγαπώ και να με αγαπούν.
User avatar
MpoMp
Venus Former Team Member
Posts: 6620
Joined: Mon Oct 04, 2010 9:31 pm
Gender:
Contact:

Re: Float και Double (Java)

Post by MpoMp » Sun Oct 24, 2010 3:55 pm

stoupeace wrote: To 3o δεν ξερω τι βγαζει η oracle απο το μυαλό της :-p
# 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ο, όπως φαίνεται απλά παραλείπεται.
Any fool can make something complicated. It takes a genius to make it simple.
MpoMp @ Discord
User avatar
Zifnab
Venus Former Team Member
Posts: 7581
Joined: Tue Nov 15, 2005 2:42 am
Academic status: MSc
Gender:
Location: Connecticut
Contact:

Re: Float και Double (Java)

Post by Zifnab » Thu Nov 04, 2010 12:16 pm

Χωρίς να είμαι σίγουρος:

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.
Post Reply

Return to “Προγραμματισμός”