• Post Reply Bookmark Topic Watch Topic
  • New Topic

Head scratcher  RSS feed

 
Ez Burdett
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I've finally decided to tackle the monster called Java. I am currently refreshing myself on the very, very basic stuff. Like the difference between double and float data types.

I've written a very, very basic program here to sort of demonstrate that distance. Here is what I have right now:

class Example3
{
public static void main(String args[])
{
int var;
double x;

var = 10;
x = 10.0;

System.out.println("Original var: " + var );
System.out.println("Original x: " + x );
System.out.println();

var = var / 4;
x = x / 4;

System.out.println("New var: " + var);
System.out.println("New x: " + x);

}

}

This compiles and runs just fine. Here's what I don't understand. My first version wouldn't compile. The only difference between the two is the colons inside the literal. I just don't understand why that would make a difference.

You guys were VERY helpful on my first attempts a couple months ago. Thanks!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ez Burdett wrote:My first version wouldn't compile. The only difference between the two is the colons inside the literal. I just don't understand why that would make a difference.

It wouldn't. Why don't you show us the other code and the error messages you were getting.
 
Ez Burdett
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, darn. That was exactly the answer I was expecting. Guess I made more than one change there, huh?
 
Ez Burdett
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pregnancy is making me stupid. I had also left out a semicolon. So that explains that LOL

Thanks!
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ez Burdett wrote: . . . the difference between double and float data types. . . .
I presume you mean int not float? That is standard integer division and has been in computing for as long as I can remember. That is why you have a % operator as well. The Java Language Specification (JLS) tells you exactly how it works,
Integer division rounds toward 0. That is, the quotient produced for operands n and d that are integers after binary numeric promotion (§5.6.2) is an integer value q whose magnitude is as large as possible while satisfying |d ⋅ q| ≤ |n|. Moreover, q is positive when |n| ≥ |d| and n and d have the same sign, but q is negative when |n| ≥ |d| and n and d have opposite signs.
…but the JLS can be difficult to understand.
 
Ez Burdett
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ever had one of those days?

That is exactly what I meant.

Thanks, guys!
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: Java is not a monster. Not unless you let it be one. You need to grab it by the throat and give it a good shake. The way you would to a lion. Then it will behave itself
 
Ez Burdett
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the encouragement!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ez Burdett wrote:Like the difference between double and float data types...

Well, the main difference is that an integer (not, as Campbell says, a float, which is like a double, only smaller) can only contain whole numbers. And that means that division has to have some sort of rules, because the result of a division often isn't a whole number.

In Java's case (which is the same as most other computer languages that have integers), they chose truncation, which is simply this:
Do the division as normal, and remove any fractional part from the result.   So:
10 / 3 = 3.3333... = 3
10 / 6 = 1.6666... = 1   (Note: Not 2. Integer division truncates; it doesn't round)

and it works whatever the sign of the result is, so:
10 / -6 = -1.6666... = -1

And BTW, I don't want to overload you too soon, but it's worth remembering that double (and float) values are usually only approximations of the value you want. It's very similar to the reason we can't represent 1/3 as an exact decimal (0.3333...), only when it comes to binary there are a lot more values we can't store exactly.

Just one such value is 0.1 (often used as a "gotcha" in programming exercises). Try defining:
private double tenth = 0.1;
then add it together ten times and print out the result. You may be surprised at what you get.

For more information, I suggest you bookmark this page. It may be a bit advanced just at the moment, but believe me, you'll be happy you did in a few month's time.

HIH

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(for the most part, ignore this post)
Winston Gutkowski wrote:...when it comes to binary there are a lot more values we can't store exactly.

really? are we sure of this? It's too early for me to try and figure out how big each infinite set is, but my intuition says they are the same size infinite...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:really? are we sure of this? It's too early for me to try and figure out how big each infinite set is, but my intuition says they are the same size infinite...

Well, I'm simply extrapolating from factors - but I could be wrong ... it's been known.

A binary-based "floating-point" value is only likely to be able to hold exact values for those that divide by 2 (or some power), or are made up of some combination of additions of those values, whereas a decimal-based "float" would naturally add 5 as a factor. I suspect that means that, in the overall scheme of real numbers, there are more that are exactly representable in decimal than in binary - but it's just a guess. I'm no mathematician.

We need Piet!

Winston
 
Tim Holloway
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:(for the most part, ignore this post)
Winston Gutkowski wrote:...when it comes to binary there are a lot more values we can't store exactly.

really? are we sure of this? It's too early for me to try and figure out how big each infinite set is, but my intuition says they are the same size infinite...


Well, yes. But Java Float and Double data isn't an infinite number of bits long. In fact, a float has only 24 bits of precision and a double has 53 bits of precision.

Precision is the number of digits after the decimal point. Or, in the case of Java/IEEE floating-point numbers, the number of bits after the binary point.

Here's an example. You have a decimal-based computer where 5 digits of precision are the limit. A number such as 1/3 can thus be approximated by: 0.33333. But that's ONLY an approximation, and doesn't distinguish 0.33333 exactly from 0.33333333 or 0.333330217. Which is also why comparison for equality on floating-point numbers must be done very carefully.

There's also a limit on the range of the exponent, so the actual set of numbers precisely representable in float or double values is far, far less than infinite. Infinitely less, in fact.

Just to confuse things, the output functions available to Java "know" that people prefer shorter decimal values to longer, more irregular ones. For example, 0.1 instead of 0.999989.. or whatever. So they'll adjust, subject to certain overridable rules. That means that the true amount of fuzz isn't as obvious.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: (...)
We need Piet!

Winston

Too much honour! Tim is at least as fine.

Another aspect of not being able to represent every value within a certain range is the following.
With doubles, we can represent up to + and - 10 ^315, IIRC. However, we only have 64 bits at
our disposal. That means that we can only have 2 ^64 different values, floating point or not.
So how come we can have a range of 10 ^315? There is no magic. We increase the range that we
can represent, at the cost that we cannot represent all values within that range.

Tim already gave an example. Another example is: if we have only 2 bits to store information,
we can represent the values 0 - 3. But by defining each of those values as being the log of
another number, we can increase the range t0 1 - 8. However, the values that can be represented
are 1, 2, 4 and 8.

And lastly: there are just as many 'binary' floating points as 'decimal' floating points! As Fred said,
infinity is infinity!

If that sounds strange, then think of Q, the set of all rationals. We know that the set of rationals
that can be represented exactly in binary is infinite, and so is the set of all rationals that can be
represented exactly in decimal format. However, both are countable, and therefore 'just as big'.
This is one of those unbelievable things. For instance: thre are just as many natural numbers as
there are squares. How can that be? Well, we can make a so called one to one relationship
between a natural number and its square, like:

1 <-> 1
2 <-> 4
3 <-> 9
4 <-> 16

ad infinitum. So, indeed, there are just as many natural numbers as there are squares...

But all is not lost! We can represent 1/3 in an exact way! Now, I forgot the name, but
there was someone of name and fame, who wrote some time ago that he was working
on a 'Rational' class, for which '1/3' would be nothing special. Who o who was he?

Greetz,
Piet
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:But all is not lost! We can represent 1/3 in an exact way! Now, I forgot the name, but
there was someone of name and fame, who wrote some time ago that he was working
on a 'Rational' class, for which '1/3' would be nothing special. Who o who was he?

Cheek! (But well remembered)

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!