473,544 Members | 1,857 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 1332
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
2744
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
1790
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...
1
1652
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...
3
4861
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
1653
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...
31
2593
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
5352
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
1346
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
5277
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
7373
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7389
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...
0
7717
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...
0
5928
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...
1
5306
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...
0
4930
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...
0
3427
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
993
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.