- #1

- 310

- 0

You are using an out of date browser. It may not display this or other websites correctly.

You should upgrade or use an alternative browser.

You should upgrade or use an alternative browser.

- Thread starter SherlockOhms
- Start date

- #1

- 310

- 0

- #2

berkeman

Mentor

- 62,247

- 13,017

It's a way to represent a wider range of numbers -- basically including exponents in the representation:

http://en.wikipedia.org/wiki/Floating_point

- #3

- 29

- 3

Sincerely,

Gary

- #4

- 310

- 0

- #5

jtbell

Mentor

- 15,866

- 4,456

Don't you have a textbook or other recommended reference source?

- #6

- 310

- 0

- #7

- 17,591

- 9,448

Surely you DO know what kind of class you are in, no? Is it a computer science class, a physics class, a math class, what???

- #8

- 310

- 0

Of course. Never said I didn't. It's a computer programming class with numerical methods involved.

- #9

- 17,591

- 9,448

Of course. Never said I didn't. It's a computer programming class with numerical methods involved.

Then read post #3

- #10

- 613

- 155

Sincerely,

Gary

A key thing to remember about floating point numbers, as used in computers, is that they are stored as approximations, unlike integers which can be represented exactly. Thus, one must never test two numbers of type float for equality (as in does "x = y", where both are floating point numbers), because even if they are so closely approximated that both print out as having a value of, say 1.3, the equality test might still fail.

Further, because of how they are stored, it is possible to have issues with overflow and loss of precision when doing calculations with them (depending on the order in which computations are done to the numbers, for example). To learn about these things, read a book on numerical computing (or wait until your professor covers them).

Handling floating point numbers correctly in programming is an important topic, and may take some time to learn.

- #11

Mark44

Mentor

- 36,019

- 7,948

Most floating point numbers are stored as approximations, but some are stored exactly, such as 1.5, 3.75, and -4.875. Any number whose fractional part is made up of a finite sum of powers of 1/2 (within limits based on the size of the mantissa) is stored exactly.A key thing to remember about floating point numbers, as used in computers, is that they are stored as approximations, unlike integers which can be represented exactly.

Thus, one must never test two numbers of type float for equality (as in does "x = y", where both are floating point numbers), because even if they are so closely approximated that both print out as having a value of, say 1.3, the equality test might still fail.

Further, because of how they are stored, it is possible to have issues with overflow and loss of precision when doing calculations with them (depending on the order in which computations are done to the numbers, for example). To learn about these things, read a book on numerical computing (or wait until your professor covers them).

Handling floating point numbers correctly in programming is an important topic, and may take some time to learn.

- #12

berkeman

Mentor

- 62,247

- 13,017

Thus, one must never test two numbers of type float for equality (as in does"x = y", where both are floating point numbers), because even if they are so closely approximated that both print out as having a value of, say 1.3, the equality test might still fail.

And remember in C at least, the check four equality would be "x == y", since == is a comparison and = is an assignment. Too easy to get those mixed up...

- #13

- 310

- 0

Thanks for all the info, guys.

- #14

- 613

- 155

I, for one, am not a guy. But I'll take the thank you generically.

- #15

- 310

- 0

'Twas meant generically.

- #16

- 310

- 0

- #17

kreil

Gold Member

- 668

- 68

Floating point numbers are defined on a grid. In other words, they have finite precision, and if a calculation falls between two floating point numbers, there will be roundoff error. Since the smallest distance between two floats is machine epsilon, the maximum value of the roundoff error (in matlab) is eps/2, or about 1.1102e-16 in double precision. Here is an example in matlab:

As mentioned in a previous post, this is why you don't compare them directly for equality, but rather to a tolerance.

Code:

```
>> A = 0.3 + 0.3 + 0.3
A =
0.9000
>> abs(A - 0.9) == eps/2
ans =
1
```

As mentioned in a previous post, this is why you don't compare them directly for equality, but rather to a tolerance.

Last edited:

- #18

- 310

- 0

Thanks for that! However, it's not so much the concept of the round off error that I'm struggling with, moreso it's calculations associated with it. I've actually posted the specific problem that I've struggled with in the homework and course work section and it'd be great if you could have a look as it'd probably give you a better understanding of what I'm struggling with. It's a worked example to do with finding the floating point representation of 0.2 and then the error between the two values.Floating point numbers are defined on a grid. In other words, they have finite precision, and if a calculation falls between two floating point numbers, there will be roundoff error. Since the smallest distance between two floats is machine epsilon, the maximum value of the roundoff error (in matlab) is eps/2, or about 1.1102e-16 in double precision. Here is an example in matlab:

Code:`>> A = 0.3 + 0.3 + 0.3 A = 0.9000 >> abs(A - 0.9) == eps/2 ans = 1`

As mentioned in a previous post, this is why you don't compare them directly for equality, but rather to a tolerance.

- #19

- 310

- 0

Share: