473,769 Members | 1,752 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 4039
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
12312
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
2978
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
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
9423
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,...
1
9995
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...
1
7410
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
6674
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
5304
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...
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.