• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

What is the variable's initial value?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Why the output is 10.5 instead of 0.0?  The f  had not been assigned with initial value -- 1 !
 
Marshal
Posts: 74341
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please avoid floats, unless some other API requires them; they simply lose you precision as against doubles.
Why would you expect that program to print 0?
 
peng shan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The default value of f is zero ,so  10.5*0 = 0.0 ,so the result is 0.0 .
 
Saloon Keeper
Posts: 13366
295
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What makes you think this application performs multiplication? I don't see the * operator anywhere in that code.
 
peng shan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In math,  a* b = ab

What does  f = 10.5f mean in java ?
 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

peng shan wrote:The default value of f is zero . . . .

No, the local variable doesn't have a default value at all; it will fail to compile if you don't initialise it. As Staphan says, you only get multiplication if you use the * operator. In fact nothing happens without an operator, if you consider the () after a method/constuctor name to be an operator. The language is different from tha language of simple algebra,.
Another reason for not using floats: the f. Look in the Java™ Tutorials for the format of different sorts of numbers.
 
peng shan
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a flag, it means that 10.5f is 10.5 float flag ,i knew now.
 
Saloon Keeper
Posts: 8704
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'f' does not stand for 'flag', it means 'float'. So, 10.5 (without the 'f') would be a double formatted floating point value of 10.5, whereas 10.5f (or 10.5F) means a "float" formatted floating point value of 10.5.
 
Saloon Keeper
Posts: 1599
51
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While we are on the topic here, when working with the long primitive type, or the Long wrapper types, we use the suffix L for literals, like 123_456_789L.

Java allows you to use "l" as well, but humans reading your code will be unsure depending on your display and settings if they are looking at:
11
or
1l

On my screen as I type this here at CodeRanch they look differently, in many other contexts they look so alike many will mistake them.

F/f if you work with floats and L/hopefully not l when working with long types are the only common suffixes for literals in Java.

But both doubles and floats also have embedded letters e or E, such as:

jshell> double c = 2.99792458E08;
c ==> 2.99792458E8


Avoid confusing that with a variable named e or E as well.
 
Ranch Hand
Posts: 106
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Please avoid floats, unless some other API requires them; they simply lose you precision as against doubles.
Why would you expect that program to print 0?



Sorry to be a nitpick, but what is the rationale for avoiding using float as opposed to double? Depending on the application, you may not require the extra precision of a double, whereas a float takes half the space. So I do not understand the blanket statement to simply avoid float. It's like saying always use int where a byte would do, no?
 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Damon McNeill wrote:. . . a float takes half the space. . . .

On a 64‑bit machine, you might use all 64 bits for floating‑point arithmetic regardless.
You will probably have to have millions upon millions of floating‑point numbers in memory all at once before you notice the difference in memory use.
The default is to use double arithmetic; indeed you either need the terminal f or a cast to turn the arithmetic into a float.
It isn't worth the bother only to lose the already limited precision of double arithmetic.

always use int where a byte would do, no?

As soon as you start doing arithmetic, your byte will be turned into an int anyway, except when using the compound assignment operators. The short and byte datatypes probably also have no real‑life use. The byte[] datatype, on the other hand, is very useful for sending information across networks.
 
Damon McNeill
Ranch Hand
Posts: 106
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Damon McNeill wrote:. . . a float takes half the space. . . .

On a 64‑bit machine, you might use all 64 bits for floating‑point arithmetic regardless.
You will probably have to have millions upon millions of floating‑point numbers in memory all at once before you notice the difference in memory use.
The default is to use double arithmetic; indeed you either need the terminal f or a cast to turn the arithmetic into a float.
It isn't worth the bother only to lose the already limited precision of double arithmetic.

always use int where a byte would do, no?

As soon as you start doing arithmetic, your byte will be turned into an int anyway, except when using the compound assignment operators. The short and byte datatypes probably also have no real‑life use. The byte[] datatype, on the other hand, is very useful for sending information across networks.



Yes, but in the FPU (as I understand it) all floats, doubles, etc. are actually promoted and stored as 128 bits to avoid loss of precision. Yet this is only temporary, as these are stored in the FPU registers, but when stored in memory (or the local variable stack) they only require the 32 or 64 bits. The same goes for byte vs int. The byte would be converted temporarily to an int in a CPU register for an operation. I guess my point is that yes there is type promotion but that only occurs temporarily within the registers of the machine and does not (at least by my understanding) affect the sizes of these objects in memory, on the stack or on the heap.

Of course, the stack layout of an architecture may just pad everything out to a 64 bit word for efficiency, but in memory, or on disk, there is a valid reason to choose the smallest data type that sufficient for the purpose.

Anyways... all this is off topic for beginning java lol. More like an Assembly language course. I would just add that a lot of the times when you need large arrays of floats (16/32 byte) for a graphics applications, it is sufficient precision and it is faster to upload an array of those guys to the GPU than double precision, where the extra precision is all zeros anyways.
 
Jesse Silverman
Saloon Keeper
Posts: 1599
51
Eclipse IDE Postgres Database C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had the discussion with Campbell in another thread some weeks back, and made almost exactly the same points you did.

I don't even know how one goes about doing the GPU-based stuff in Java, the material I worked with was focused on C/C++/Python and FORTRAN only.

There is some vector API work that has made its way into Java 17 for future progress in this direction.

I agree with Campbell's main point that short of dealing with HUGE numbers of data, there is very little to be bought using float type, at least on the CPU-based and FPU-based instructions that I think Java uses for all normal math.

And yes, this is all way, way beyond the scope of Beginning Java.

Cheers!
 
Damon McNeill
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:I had the discussion with Campbell in another thread some weeks back, and made almost exactly the same points you did.

I don't even know how one goes about doing the GPU-based stuff in Java, the material I worked with was focused on C/C++/Python and FORTRAN only.

There is some vector API work that has made its way into Java 17 for future progress in this direction.

I agree with Campbell's main point that short of dealing with HUGE numbers of data, there is very little to be bought using float type, at least on the CPU-based and FPU-based instructions that I think Java uses for all normal math.

And yes, this is all way, way beyond the scope of Beginning Java.

Cheers!



Yes I agree, lacking other reason,  using double over float may or may not  avoid some precision issues. I still can't agree it should just be the default though. There is a reason for the different choices of types, after all.
 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Damon McNeill wrote:. . . . There is a reason for the different choices of types, after all.

Is that reason historical, that Java® was inspired by older languages, developed in the days when 8 bits of memory saved really meant something?
 
Marshal
Posts: 26908
82
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Is that reason historical, that Java® was inspired by older languages, developed in the days when 8 bits of memory saved really meant something?



For example: consider the java.awt.Font class. Yes, it's for representing fonts. The point size of a Font is stored internally as a float value; it's very often an integer but sometimes you get a 9.5-point font or some other non-integral value. But the point size of a font is never going to be a number where the precision difference between using float or double to represent it is going to make any practical difference. Sure, 8.3 is represented differently by float and double but the difference is too small to matter when you use that font to display some text on a screen.

And yes, back in the days when Font was written it did matter whether you could save 4 bytes of memory.
 
Jesse Silverman
Saloon Keeper
Posts: 1599
51
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As much as everyone here loves BigDecimal and as far away from the original poster's confusion that sparked the thread as can be, a couple of links I found that didn't answer the "When does it make sense to use float type in Java in preference to double?" as well as some others but were extremely interesting for those who care about floating point:

https://www.intel.com/content/dam/www/public/us/en/documents/case-studies/floating-point-case-study.pdf

http://people.eecs.berkeley.edu/~wkahan/DearSara.txt
and others by this guy behind much of the floating point standards contents:
http://people.eecs.berkeley.edu/~wkahan/

This actually showed that at that time on that hardware, yes, single-precision can sometimes be faster than double-precision, I didn't pay close enough attention to see if that was because there was "lots of data" I suspect that is the case:
https://wiki.tuflow.com/index.php?title=Hardware_Benchmarking_Topic_Single_Precision_VS_Double_Precision

Also to the original question:
https://softwareengineering.stackexchange.com/questions/188721/when-do-you-use-float-and-when-do-you-use-double

 
Bartender
Posts: 4666
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Besides, if you use Processing (see Processing.org) then you notice that floats are standard, and I've seen no example whatsoever where that leads to any problem at all.
 
Jesse Silverman
Saloon Keeper
Posts: 1599
51
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Piet:

That site/software looks nice, but I am not sure that they mean for that to be scaled for things like population studies, statistical mechanics, astrophysics, etc.

A lot of work went into the IEEE standards (followed by Java, now strictly by default) so that if you run any number of the same calculations on the same data, even if there are lots of data and lots of calculations, on either floats or doubles, anywhere, on any hardware, you will get the same results.

There are still situations where running lots of calculations on lots of data using floats can get you significantly different results from what you would get if you used doubles thru-out, at least it won't vary by hardware platform.

In many applications, usually cited as graphics or games being the main domains, this is unlikely to matter.

I know when I was reading AI stuff on GPU's, they would tell you to run your simulations (for lack of a better word, I am still a beginner there) and to see if you got similar results using float16, float32 and float64, and then use the least precision that seemed to get you the same results.

I think just by virtue of the fact that you know and use R regularly, that I would defer to you in areas of discrete mathematics.

But the pundits I listen to that make me more wary of using float, even when the quantities I am putting into the calculation are only known to within the precision of float anyway, seem to say that among the common mass of programmers who are doing floating point arithmetic, it is a relative minority that are good at telling whether they are going to get meaningfully different results from using floats in lieu of doubles somewhere.  Understanding the ways in which errors propagate and such is "too much maths" (not for the people writing, but for the audience they are trying to target, who will write far more calculations than the authors).  I am sure that among, for example, those who realize the utility of using logarithms to deal with avoiding overflow and underflow while dealing with very large and very small quantities respectively, or grouping calculations differently to avoid loss of precision, the decision will be a more informed one.

But I am pretty aware of such issues, and not always sure I would recognize where choosing different precisions of float types would affect my results, except by running experiments and looking at the results.  No, I am sure I would guess wrong without doing extensive analysis of a sort I am not used to doing, despite having been exposed to the topic at various points in my career.

All of this is way, way beyond Beginning Java, but an important topic in computing that I intend to pay attention to for my whole career.

At one end of things we have "Forget performance, just use BigDecimal" it seems.  At the other end, we have "You can get faster performance if you use float instead of double (Java terms) when it doesn't make much difference".

I still think that giving beginners as a rule of thumb "For currencies and monetary values, use BigDecimal or int counting pennies, otherwise use double unless someone on your team that knows error analysis and computational math very well tells you to try getting away with floats or even float16 (not available directly in Java)" seems pretty reasonable, no?
 
Piet Souris
Bartender
Posts: 4666
183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I always use doubles, that is not a problem. What I sometimes find a problem around here, is that personal opinions are being sold as if they were the truth, the whole truth and nothing but the truth.
 
Jesse Silverman
Saloon Keeper
Posts: 1599
51
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I like to think that I am usually careful to state that something I am saying is my opinion, and furthermore (tho it takes more words) whether that is coming from years of personal experience at work, or whether I am just parroting some source I trust highly, (usually because they have hundreds of times more experience than I do in some area).

Appeal to authority is one of the classical logical fallacies!

But I am guilty of it in this domain.

At least in terms of "If I am doing maths, for a project that might kill people if my calculations go wrongly, and Kahan would roll his eyes at me, I'd get very nervous."

I don't know why, but I just looked back because what you said triggered some half-suppressed memory and now realize this dude that I'd been quoting is THIS one:
http://people.eecs.berkeley.edu/~wkahan/JAVAhurt.pdf

"C'mon, Bill, tell us how you really feel!  Stop holding back."

Anyway, while I already try not to push my (currently-held) opinions as absolute facts, I will re-new my commitment to citing sources or reasonings where appropriate.

Oh yeah, in the "jealous of people who seem to be able to learn way more languages than I can at once without it all becoming a jumbled, useless pile of mush":
https://0.30000000000000004.com/

I bet he has all 3 of your favorite languages there.
 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:. . . https://0.30000000000000004.com/ . . . .

What a splendid website! One of the few places, where, more by luck than good management, the greater imprecision of a float hides the errors.
 
Jesse Silverman
Saloon Keeper
Posts: 1599
51
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't have enough time to spend there to see if it is the case there, but your reply reminds me of colleagues who knew how to make the same basic errors in two dozen languages.

And I was still sort of jealous.  Information Overload to the max.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic