Ez Burdett

Greenhorn

Posts: 10

posted 3 years ago

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!

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

Ez Burdett

Greenhorn

Posts: 10

Ez Burdett

Greenhorn

Posts: 10

Campbell Ritchie

Marshal

Posts: 56599

172

posted 3 years ago

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,Ez Burdett wrote: . . . the difference between double and float data types. . . .

…but the JLS can be difficult to understand.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.

Ez Burdett

Greenhorn

Posts: 10

Campbell Ritchie

Marshal

Posts: 56599

172

posted 3 years ago

Well, the main difference is that an

In Java's case (which is the same as most other computer languages that have integers), they chose

Do the division as normal, and

and it works

And BTW, I don't want to overload you too soon, but it's worth remembering that

Just one such value is 0.1 (often used as a "gotcha" in programming exercises). Try defining:

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

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

posted 3 years ago

(for the most part, ignore this post)

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 wrote:...when it comes to binary there are alotmore 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...

posted 3 years ago

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

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

posted 3 years ago

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.

fred rosenberger wrote:(for the most part, ignore this post)

Winston Gutkowski wrote:...when it comes to binary there are alotmore 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

posted 3 years ago

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

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 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

posted 3 years ago

Cheek! (But well remembered)

Winston

Piet Souris wrote:But all is not lost! Wecanrepresent 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. |