473,385 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Strange behaviour when using floats

Hi,

I have the following very simple code snippet:

line 1: float f;
line 2: f = 100000.99f;

Why is f: 100000.992 after line 2? I would assume it to be 100000.99.

Cheers
Henrik.
Sep 16 '08 #1
8 1319
on 16-9-2008, Henrik Skak Pedersen supposed :
Hi,

I have the following very simple code snippet:

line 1: float f;
line 2: f = 100000.99f;

Why is f: 100000.992 after line 2? I would assume it to be 100000.99.

Cheers
Henrik.
See http://www.yoda.arachsys.com/csharp/floatingpoint.html
Sep 16 '08 #2
Hi Hans,

Thank you for your reply. So I guess that decimal is my choice then.

Cheers
Henrik.

"Hans Kesting" <ne*********@spamgourmet.comwrote in message
news:uz****************@TK2MSFTNGP03.phx.gbl...
on 16-9-2008, Henrik Skak Pedersen supposed :
>Hi,

I have the following very simple code snippet:

line 1: float f;
line 2: f = 100000.99f;

Why is f: 100000.992 after line 2? I would assume it to be 100000.99.

Cheers
Henrik.

See http://www.yoda.arachsys.com/csharp/floatingpoint.html


Sep 16 '08 #3
Henrik Skak Pedersen wrote:
Hi Hans,

Thank you for your reply. So I guess that decimal is my choice then.

Cheers
Henrik.

"Hans Kesting" <ne*********@spamgourmet.comwrote in message
news:uz****************@TK2MSFTNGP03.phx.gbl...
>on 16-9-2008, Henrik Skak Pedersen supposed :
>>Hi,

I have the following very simple code snippet:

line 1: float f;
line 2: f = 100000.99f;

Why is f: 100000.992 after line 2? I would assume it to be 100000.99.

Cheers
Henrik.
See http://www.yoda.arachsys.com/csharp/floatingpoint.html

You can use a double if you just want better precision, but still want
the performance of a type that is supported by the ALU of the processor.

A decimal still doesn't represent values exactly, but you get something
like 100000.990000002 rather than 100000.992.

--
Göran Andersson
_____
http://www.guffa.com
Sep 16 '08 #4
On Sep 16, 12:49*pm, Göran Andersson <gu...@guffa.comwrote:
You can use a double if you just want better precision, but still want
the performance of a type that is supported by the ALU of the processor.

A decimal still doesn't represent values exactly, but you get something
like 100000.990000002 rather than 100000.992.
Both double and decimal represent exact values - just different sets
of values. And decimal certainly *can* represent 10000.992 exactly.

Jon
Sep 16 '08 #5
On Tue, 16 Sep 2008 06:22:40 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
>A decimal still doesn't represent values exactly, but you get something
like 100000.990000002 rather than 100000.992.

Both double and decimal represent exact values - just different sets
of values. And decimal certainly *can* represent 10000.992 exactly.
Or even 100000.99, as seen in the original post. :)
Sep 16 '08 #6
Thank you all for your replies.

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Tue, 16 Sep 2008 06:22:40 -0700, Jon Skeet [C# MVP] <sk***@pobox.com>
wrote:
>>A decimal still doesn't represent values exactly, but you get something
like 100000.990000002 rather than 100000.992.

Both double and decimal represent exact values - just different sets
of values. And decimal certainly *can* represent 10000.992 exactly.

Or even 100000.99, as seen in the original post. :)

Sep 17 '08 #7
Jon Skeet [C# MVP] wrote:
On Sep 16, 12:49 pm, Göran Andersson <gu...@guffa.comwrote:
>You can use a double if you just want better precision, but still want
the performance of a type that is supported by the ALU of the processor.

A decimal
Obviously I meant a double.
still doesn't represent values exactly, but you get something
>like 100000.990000002 rather than 100000.992.

Both double and decimal represent exact values - just different sets
of values.
What do you mean by that? A double is a floating point type, and
approximates most values.
And decimal certainly *can* represent 10000.992 exactly.
A decimal stores 100000.99 as 100000.99, not as 100000.992.

--
Göran Andersson
_____
http://www.guffa.com
Sep 17 '08 #8
Göran Andersson <gu***@guffa.comwrote:
Jon Skeet [C# MVP] wrote:
On Sep 16, 12:49 pm, Göran Andersson <gu...@guffa.comwrote:
You can use a double if you just want better precision, but still want
the performance of a type that is supported by the ALU of the processor.

A decimal
Obviously I meant a double.
Sorry, it wasn't obvious to me. I thought you really were casting
aspersions on decimal :)
still doesn't represent values exactly, but you get something
like 100000.990000002 rather than 100000.992.
Both double and decimal represent exact values - just different sets
of values.
What do you mean by that? A double is a floating point type, and
approximates most values.
Decimal is a floating point type too - just a floating decimal point
instead of a floating binary point. Both types have a set of numbers
they can represent exactly. You can't represent 1/3 exactly in decimal
any more than you can represent 1/5 exactly in binary.

The reason that decimal "feels" precise whereas binary "feels"
approximate is that humans tend to use a decimal system - we tend to
think that 1/5 is a more "normal" number than 1/3. When you look at it
in a mathematical sense, decimal has more precision and less range than
double, but both are just sets of numbers with conversions and
operations which will approximate the accurate value to the nearest
value in the target set.
And decimal certainly *can* represent 10000.992 exactly.
A decimal stores 100000.99 as 100000.99, not as 100000.992.
Indeed - I didn't check the original post, just yours.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 17 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
by: Jordan Rastrick | last post by:
Can anybody please give me a decent justification for this: class A(object): def __init__(self, a): self.a = a def __eq__(self, other): return self.a == other.a s = A(3)
0
by: h | last post by:
Hello everyone I am baffled by the behaviour of a webpage that I am trying to design(when viewed in ie6). I believe the problem arises when I use floats(at least that is the only occasion...
1
by: David Ehmer | last post by:
Within divs my pages are rendering with the 2nd and subsequent elements indented slightly. eg the first paragraph, list item or heading is where it should be, all the rest in the div (whether...
3
by: Sebastian C. | last post by:
Hello everybody Since I upgraded my Office XP Professional to SP3 I got strange behaviour. Pieces of code which works for 3 years now are suddenly stop to work properly. I have Office XP...
11
by: Marlene Stebbins | last post by:
Something very strange is going on here. I don't know if it's a C problem or an implementation problem. The program reads data from a file and loads it into two arrays. When xy, x, y, *xlist and...
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
5
by: Vedran FuraÄ | last post by:
I think that this results must be the same: In : math.atan2(-0.0,-1) Out: -3.1415926535897931 In : math.atan2(-0,-1) Out: 3.1415926535897931 In : -0 == -0.0 Out: True
4
by: Rico | last post by:
Hi Folks! I have a strange thing happening; I have a field that Counts the number of records and another field that shows the number of clients (Count(RecordID)=3 and NoOfClients=2) When I do a...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.