Click here to Skip to main content
15,903,632 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to calculate the sine values in vb.net???
The values should be lie between -1 and 1.
I write the code as follows in vb.net


Label1.Text= Math.Sin(Math.PI)


On button click, the result is shown on Label1 is : 1.2246063638223

The problem is that the values should lie between -1 and 1. But in vb.net, values are greater than 1 like above. When I check the same above sine value in Matlab:

sin(pi) 


The result is: 1.2246e-016

Which is between -1 and 1

When i check the same on calculator. again the value is between -1 and 1, then how to resolve my problem in vb.net.
Posted

The answer you're actually seeing is 1.22460635382238 E-16

You might find your label is not wide enough and has AutoSize = False
However, .Net Math.Sin expects the angle to be in radians, what does tyour Math.PI represent?
The conversion is [angle in degrees] * (Math.PI / 180) = radians.

Mike
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Mar-13 16:05pm    
Good catch, my 5. I also added a solution about formatting, which is important in this case.
—SA
[no name] 25-Mar-13 16:25pm    
Thanks for your information.. It is also a good to know new about something...
Sergey Alexandrovich Kryukov 25-Mar-13 16:37pm    
Sure. I think you could accept my solution as well; and you should use formatting in your case...
—SA
The Solution 1 and 2 are correct. In most cases, you don't need to output value in its default format.

First of all, you assign a double value to a string property. Such assignment makes no sense in .NET, but VB.NET provides "automatic" work-around by assigning not double value, but double.ToString(), which is actually a bad thing; it would be much better it if issued a compilation error instead, as it is normally done.

You should always use ToString, which also allows you, in other forms of this function, to provide culture, format string, of both. Please see: http://msdn.microsoft.com/en-us/library/system.double.tostring.aspx[^].

For format strings and their effect, please see:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

—SA
 
Share this answer
 
Your label is too short - it is actually displaying 1.2240635382238E-16
 
Share this answer
 
Comments
[no name] 22-Mar-13 2:47am    
Thanks......... It is actually displaying the value you told when I resize the Label..
Sergey Alexandrovich Kryukov 25-Mar-13 15:57pm    
You did not tag your UI library, but System.Windows.Forms.Label has AutoSize option.
—SA
Sergey Alexandrovich Kryukov 25-Mar-13 16:04pm    
Usually, you also should control formatting of your string presentation of the numbers you show on screen.
Please see my answer.
—SA
Sergey Alexandrovich Kryukov 25-Mar-13 16:05pm    
Good catch, my 5. I also added a solution about formatting, which is important in this case.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900