473,782 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

1.9489 - System.Math.Tru ncate(1.9489) = 0.9489000000000 0008

Try
Jun 27 '08 #1
5 4040
Try it in immediate window
1.9489 - System.Math.Tru ncate(1.9489)

Result is
0.9489000000000 0008

Am I doing something wrong?
Jun 27 '08 #2
What do expect the result to be?
"Aamir Mahmood" <aa**********@g mail.comwrote in message
news:%2******** **********@TK2M SFTNGP04.phx.gb l...
Try it in immediate window
1.9489 - System.Math.Tru ncate(1.9489)

Result is
0.9489000000000 0008

Am I doing something wrong?

Jun 27 '08 #3
Aamir Mahmood <aa**********@g mail.comwrote:
Try it in immediate window
1.9489 - System.Math.Tru ncate(1.9489)

Result is
0.9489000000000 0008

Am I doing something wrong?
You're expecting decimal numbers to be exactly represented as doubles.

The literal 1.9489 ends up with a double of this exact value:
1.9489000000000 000767386154620 908200740814208 984375

The immediate window happens to print out lots of digits - a simple
call to Console.WriteLi ne doesn't. Which is the most appropriate action
really depends on what you're doing.

See http://pobox.com/~skeet/csharp/floatingpoint.html for more
information.

--
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
Jun 27 '08 #4
Aamir Mahmood wrote:
Try

What is the question?

If you google for floating point errors, you should find tons of
information about this problem.

In short, a floating point value stored in a N-bit register is always
going to be an approximation of the real value. You will experience both
the case you see, that the value is slightly higher than what you expect
it to be, and the opposite, that it is slightly lower.

This is normal, can't be fixed, except for using a type with higher
precision and you must deal with it in your code.

As a quick-fix, try using the Decimal type.

--
Lasse Vågsæther Karlsen
mailto:la***@vk arlsen.no
http://presentationmode.blogspot.com/
PGP KeyID: 0xBCDEA2E3
Jun 27 '08 #5
MC
You've just bumped into a fundamental fact about computers -- They use binary arithmetic and do not have exact representations for some decimal numbers. For example, 0.5 is represented exactly but 0.1 is not.

This is exactly like the problem of representing 1/3 in decimal. 0.3 is close, 0.33 is closer, 0.333 is closer yet, but no finite number of 3's is enough.

The same thing happens with the binary digits when you are representing 0.1, or 0.9489. So the computer gives you an approximation.

The C# Decimal type actually represents numbers in base 10, the same way we do arithmetic with pencil and paper, but computation is slower that way.
Jun 27 '08 #6

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

Similar topics

47
12327
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) { int result = 0; for(int i = 1;i <=iterations;i++) { result += i;
2
402
by: Murphy | last post by:
Is it possible using the validation controls to ensure the user enters either a phone or mobile number and that it is exactly 10 numeric characters ? Thanks Murphy
10
1417
by: Dica | last post by:
i've used threads in a couple of c# desktop apps with no problems before, but can't seem to get this code to update my listBox: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim t As Thread t = New Thread(AddressOf Me.BackgroundProcess)
3
2376
by: Stu | last post by:
Hi, I am trying to delete a file but get the error message 'The process cannot access the file "C:\MyFile.txt" because it is being used by another process'. Is there any way to delete this file anyway? Thanks in advance,
9
5204
by: Adam Right | last post by:
Hi, Anyone suffers from the high memory usage of C# in windows applications? Is there a solution for that problem? Thanks...
4
11187
by: mps | last post by:
It seems to me that the MSDN code for synchronizing a producer and consumer thread at http://msdn2.microsoft.com/en-us/library/yy12yx1f.aspx is completely wrong. There is no reason to assume that each call to NewItemEvent.Set() will release a consumer thread (as explicitly mentioned in the AutoResetEven documentation), which means that the consumer threads won't necessarily know that there are items on the queue. Is this correct? Mike
2
1074
by: nomail | last post by:
Since no one I know at work or at home gets this, I had to share it with someone who would appreciate it as much as I did. Sorry for being off topic but we all need a good smile now and then. http://imgs.xkcd.com/comics/commented.png I'd love to start doing that to people at work who ask for computer help but I'd hear about it from higher up eventually! ;)
9
1527
by: =?Utf-8?B?UmF5?= | last post by:
Have SQL server on one machine. Have IIS on another machine in same large intranet. Have website in IIS with Basic Authentication turned on and other options deselected. Have webpage (.aspx) with following connection in the web.config file <add name="NorthwindConnectionString1" connectionString="Data Source=VHAV16PLAN1;Initial Catalog=Northwind;Integrated Security=SSPI" providerName="System.Data.SqlClient" /> Have following added to...
2
2980
by: rahul | last post by:
Hello all I have written a chat server. It is invoked as follows - $ ./server -start It becomes a daemon. I want to implement a stop call- $ ./server -stop The approach I am following is to write the PID of the running server in a flat file when server starts. When it needs to be stopped, kill(SIGUSR1, pid) is sent to the server. The signal handler closes all the open sockets and exits.
0
9643
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
9480
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8968
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
7494
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
5378
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.