Is Math.Sin giving me the wrong answer? [duplicate] - c#

This question already has answers here:
Math.Cos & Math.Sin in C#
(7 answers)
Closed 9 years ago.
I am trying to calculate sin(pi radians) using Math.Sin(). I am using the following code.
double degrees = 180;
double radians = Math.PI / 180 * degrees;
Console.WriteLine(Math.Sin(radians));
It is giving me a value of
1.22460635382238E-16
What is the reason for this? Surely it should be returning a value of 0 right?
Please enlighten me. Thanks.

1.22460635382238E-16 basically is 0. it actually represents the number
0.000000000000000122460635382238. This number is close to 0. It's basically just because of different rounding errors all coming together.
take a look at http://en.wikipedia.org/wiki/Floating_point for more information.

Related

Is there a way to display 2 decimal places instead of automatically rounding? [duplicate]

This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 3 years ago.
I'm making a simple calculator in c# and and everything works well until I try dividing numbers that should give decimal places. How to display 2 decimal places in these cases?
I've tried putting #.## after .ToString.
{label1.Text = (divide / Convert.ToInt64(label1.Text)).ToString("#.##");}
I expect the output of 5/4 to be 1.25, but it is 1.
convert the number to float instead of int,
{label1.Text = ((float)divide / float.Parse(label1.Text)).ToString("n2");}

Math.Ceiling Tips [duplicate]

This question already has answers here:
C# decimal take ceiling 2
(5 answers)
Closed 7 years ago.
How would I round up the amount of 3.79 to 3.80?
double number = Math.Ceiling(3.79); //Gives me 4, but I need 3.80
It's a simple question, but I do not know how to achieve it. Thank you in advance.
How about using Math.Round(double, int) overload with 1 digit?
Math.Round(3.79, 1) // 3.8
Are you looking for
double number = Math.Ceiling(3.79 * 10.0) / 10.0;
if you want to Round (not find the Ceiling)
double number = Math.Round(3.79, 1);
Please, note the difference:
Ceiling(32.1) == 33
Round(32.1) == 32

Math.Abs weird behaviour [duplicate]

This question already has answers here:
Double vs Decimal Rounding in C#
(2 answers)
Closed 7 years ago.
I am doing following equation but the result is not as expected
double dasdas = Math.Abs(3.2 - 1.9);
The result is
1.3000000000000003
However the correct result should be
1.3
What may be the reason of this?
c# 4.5.2
This is because you're using a double - which is a floating point. These by definition this cannot store the exact number.
You need to use Decimal.
Take at look at here and What Every Computer Scientist Should Read About Floating Point
Use decimal:
decimal dasdas = Math.Abs(3.2m - 1.9m);

Rules for counting with parentheses C# [duplicate]

This question already has answers here:
C# simple divide problem
(9 answers)
Closed 7 years ago.
This mabey is a dumb question but i try to count discount on a price like this:
newAllaVaror.Pris = System.Convert.ToDouble(
(1 - (clientKampanj.VisaKampanj(vara.ProduktNamn) / 100)) * vara.Pris
).ToString();
It will look like this in reality (1-(20/100)*7.99), but my output is now 7.99 but it should be 6,392..becuse the orginal price is 7.99...
I have tried to move the parentheses but i only get 1 then..
In wich order does C# go thru the parentheses, becuse this should work right??
I'm pretty sure you are doing an integer division here:
(clientKampanj.VisaKampanj(vara.ProduktNamn) / 100)
So if you have 20/100, it will result in 0 instead of the expected 0.2 as the reminder is truncated.
You need to convert to double, one of the operands:
(clientKampanj.VisaKampanj(vara.ProduktNamn) / 100.0)
Since #Scott pointed out your other variables are of type decimal, you need to convert one of the operands to that type instead:
(clientKampanj.VisaKampanj(vara.ProduktNamn) / 100.0m)

taking just two digits of a Number of type double without rounding it [duplicate]

This question already has answers here:
Truncate Two decimal places without rounding
(24 answers)
Closed 8 years ago.
I have this Number
double Nb=4.9584763251
how can I make get only two digits after the comma without rounding the number :
Nb=`4.95` and not `4.96`
using the Math.Round(Nb,2) will return 4.96
is there any way to fix this ?
You may use Math.Truncate(Nb * 100) / 100m
double x = ((int)(Nb*100))/100.0

Categories