473,769 Members | 6,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1345
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*********@sp amgourmet.comwr ote in message
news:uz******** ********@TK2MSF TNGP03.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*********@sp amgourmet.comwr ote in message
news:uz******** ********@TK2MSF TNGP03.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.99000000 2 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.co mwrote:
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.99000000 2 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.co m>
wrote:
>A decimal still doesn't represent values exactly, but you get something
like 100000.99000000 2 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*********@nn owslpianmk.comw rote in message
news:op******** *******@petes-computer.local. ..
On Tue, 16 Sep 2008 06:22:40 -0700, Jon Skeet [C# MVP] <sk***@pobox.co m>
wrote:
>>A decimal still doesn't represent values exactly, but you get something
like 100000.99000000 2 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.co mwrote:
>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.99000000 2 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.co mwrote:
Jon Skeet [C# MVP] wrote:
On Sep 16, 12:49 pm, Göran Andersson <gu...@guffa.co mwrote:
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.99000000 2 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.co m>
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
2786
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
1808
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 when I notice it). My page is set up so that I have a content box centered, and a box to create an interior margin(both divs). I also want to have a third box which would float to the right. For some
1
1661
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 they are img, h2, p, li etc) are indented a small amount. If you have IE5.2Mac see the link below for an example of this. Note these divs mostly aren't positioned. ie they just sit in the normal flow. Seems to be a problem specific to this...
3
4874
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 Developer (SP3 for Office, SP1 for developer, JET40SP8) on Windows XP Home Edition (SP1). The same behaviour occurs on Windows 98 too.
11
1676
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 *ylist are ints or floats there is no apparent problem. If these variables are doubles, the program crashes. Furthermore, the crashes occur only when xlist and ylist are free()ed. When the above variables are doubles and the calls to free() are...
31
2636
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
5366
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
1360
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 simple expression "Count(RecordID)/NoOfClients" the expected result should be 1.5. Instead the result I get is 1. Any ideas? Thanks! Rick
8
5317
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. (Sorry, long email) The first two examples are behaving normal, the thirth is strange....... I wrote the following flabbergasting code: #-------------------------------------------------------------
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7409
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.