• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

printing float?

 
Ranch Hand
Posts: 271
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(4 + 1.0f); would print 5.0   (Even with strictfp, these are my results as well.)
Class PrintStream


println
public void println(float x)
Prints a float and then terminate the line. This method behaves as though it invokes print(float) and then println().
Parameters:
x - The float to be printed.



print
public void print(float f)
Prints a floating-point number. The string produced by String.valueOf(float) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.
Parameters:
f - The float to be printed
See Also:
Float.toString(float)


However, my expectations were  4.0f + 1.0f ->5.0f?  Why are the results 5.0 (especially since this is not a float)?  How is printing 5.0f achieved?
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Charles O'Leary wrote:
However, my expectations were  4.0f + 1.0f ->5.0f?  Why are the results 5.0 (especially since this is not a float)?  How is printing 5.0f achieved?



Printing a "float" in this case, means printing a floating point number. The syntax used is either the scientific notation (with a decimal point and an E for the "times ten to"), or the standard floating point (with just the decimal point). Neither syntax has an "f". If you want the "f", you can print it out yourself.

Henry
 
Charles O'Leary
Ranch Hand
Posts: 271
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:
Printing a "float" in this case, means printing a floating point number. The syntax used is either the scientific notation (with a decimal point and an E for the "times ten to"), or the standard floating point (with just the decimal point). Neither syntax has an "f". If you want the "f", you can print it out yourself.
Henry


Although I had anticipated that as a possible reply, isn't this highlighting a possible API errata (since there appears to be a clear contradiction)?  


println
public void println(float x)
Prints a float and then terminate the line. This method behaves as though it invokes print(float) and then println().
Parameters:
x - The float to be printed.



print
public void print(float f)
Prints a floating-point number. The string produced by String.valueOf(float) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.
Parameters:
f - The float to be printed
See Also:
Float.toString(float)


Secondly, as you mentioned, I take it that there's no automatic way to print "f" using the "+" operator (nor the "/" operator), since even an explicit float cast results in the String representation of a double.  Correct?    


enthuware.ocajp.i.v8.2.1277:  Note that System.out.println((float)5/4); will print 1.25. If you remove the explicit cast (float), it will print 1.  

 
Henry Wong
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Charles O'Leary wrote:
Although I had anticipated that as a possible reply, isn't this highlighting a possible API errata (since there appears to be a clear contradiction)?  



Can you elaborate? What contradiction are you referring to here?  It is certainly not very clear, if at all.

Henry
 
Charles O'Leary
Ranch Hand
Posts: 271
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:

Charles O'Leary wrote:
Although I had anticipated that as a possible reply, isn't this highlighting a possible API errata (since there appears to be a clear contradiction)?  



Can you elaborate? What contradiction are you referring to here?  It is certainly not very clear, if at all.

Henry


Sybex OCA Java 8 Study Guide's attachment shows a float(in bold in the quoted API definition) as 32-bits, while a floating point(in bold in the quoted API definition) could be either a 32-bit float or a 64-bit double.  Yet, it appears that even an explicit float cast results in the String representation of a double being printed, which is a contradiction from "Prints a float and then terminate the line" for the API definition of println?

More importantly, since I doubt that there's going to be (or needs to be) a change with the API, do you know how one could automatically print the "f" using "public void println(float x)" (as the API definition suggests)?  From what we have been discussing, there's no why.  Correct?
JavaPrimitiveTypes.png
[Thumbnail for JavaPrimitiveTypes.png]
 
Henry Wong
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Floating point numbers, when printed, are *not* 32 bit or 64 bit. They are simply output -- that adheres to a particular format.

And there is no information in that format that indicates that the original number, that was used for the print out, was 32 bit or 64 bit. In other words, printing a 32 bit float type, and having it without the "f", does not mean it got cast to a double.

Henry
 
Charles O'Leary
Ranch Hand
Posts: 271
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Henry Wong wrote:Floating point numbers, when printed, are *not* 32 bit or 64 bit. They are simply output -- that adheres to a particular format.

And there is no information in that format that indicates that the original number, that was used for the print out, was 32 bit or 64 bit. In other words, printing a 32 bit float type, and having it without the "f", does not mean it got cast to a double.

Henry



Subex

literals are assumed to be double, unless postfixed with an f, as in 2.1f



I just find it "interesting" in light of the API and floating point default.  Thanks for the confirmation Henry.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!