float value was not correct C# in unity3d [duplicate] - c#

This question already has answers here:
How to round a float value in c#?
(3 answers)
Closed 6 years ago.
I made a test in C# with unity3d engine.
like this:
float dt = 22.05f + 0.05f;
dt value should be 22.1f but i debug dt value in VS2015,dt value is 22.0999985f. I don't know why it was not the correct value I expected.

Because floats are binary - you can not express every decimal number exactly. Not on the fraction side, where it has to be constructed out of parts that are binary: 1/2, 1/4, 1/8 etc.
.1 (1/10th) can only be approximated.
This leads to all kinds of small issues that programmers - mostly those who refuse to learn programming by reading books that explain things in detail - fall into.
Another item is that you should not never ever compare floats to a constant - if the float is calculated, it may be SLIGHTLY off. You always compare whether the difference between two values is smaller than a given small epsilon (Abs(a-b)

Related

Why does my double round my variables sometimes? [duplicate]

This question already has answers here:
Difference between decimal, float and double in .NET?
(18 answers)
Closed 3 years ago.
I really dont unterstand why, but my double array just sometimes round my variables, even though it shouldn't. The weird thing is, that it only does this sometimes, as you see in the picture. All of the array elements should have this long precision. The Variable "WertunterschiedPerSec" is also at this precision every time, but still, if i add it to Zwischenwerte[i], then it sometimes just get less precisie, even though i dont do anything anywhere. Does anybody know why?
I would suggest using a decimal, but let's get into the exact details:
double, float and decimal are all floating point.
The difference is double and float are base 2 and decimal is base 10.
Base 2 numbers cannot accurately represent all base 10 numbers.
This is why you're seeing what appears to be "rounding".
I would use the decimal variable instead of double because you can basically do the same functions as a double except for using the Math function. Try to use Decimals and if you need to convert than use:
Double variable = Convert.ToDouble(decimal var);
Decimals are meant for decimals so they will hold more information than a float or decimal

C# float.Parse losing precision [duplicate]

This question already has answers here:
c# float [] average loses accuracy
(3 answers)
Why is my number being rounded incorrectly?
(5 answers)
Closed 5 years ago.
I'm having some trouble to convert data taken in my database to float numbers. Basically, the application is rounding one of my values to one decimal house. The table contains a set of fields with monetary values and one of these values te application keeps rounding. The strangest thing is that, despite the fact that there are more float values on my database, just one of them is rounded (valorAtualizadoAcordo). So, this is my query:
SELECT
idAcordo,
tipoAcordo,
valorAtualizadoAcordo,
dataAtualizacaoAcordo,
valorConfessadoAcordo,
valorAcordo,
valorEntradaAcordo,
valorParceladoAcordo,
quantidadeParcelasAcordo,
taxaAcordo,
atualizacaoTrAcordo,
dataPrimeiraParcelaAcordo,
dataUltimaParcelaAcordo,
dataAssinaturaAcordo,
dataAprovacaoAcordo,
primeiraDataAbertoAcordo,
dataProtocoloAcordo,
tipoProtocoloAcordo,
cadastroDspsAcordo,
dataSolicitacaoAcordo,
contabilizadoAcordo,
dataLancamentoAcordo,
contratoDevolvidoAcordo,
dataDevolucaoAcordo,
volumetriaAcordo,
pagamentoRealizadoDataAcordo,
contratoAntesAjuizamentoDataAcordo
FROM
acordo
WHERE
FK_idContrato = 46;
And this is my convertion:
ac.setValor(float.Parse(acd.Rows[i][5].ToString(), CultureInfo.InvariantCulture));
The value I'm trying to retrieve from the database is 325,360.69. I'm converting it to PT-BR format, what gives me "325360,7" (in Brazil, we use "," instead of "." to define the decimal floating point).
Not sure why you think making a string from a float and back is a good idea, but I would start by dropping the ToString. The use a cast or a real conversion method like convert.ToFloat:
float f = (float)acd.Rows[i][5];
Or:
float f = Convert.ToFloat(acd.Rows[i][5]);

Why is (1/90) = 0? [duplicate]

This question already has answers here:
C# is rounding down divisions by itself
(10 answers)
Closed 8 years ago.
I am working in Unity3D with C# and I get a weird result. Can anyone tell me why my code equals 0?
float A = 1 / 90;
The literals 1 and 90 are interpreted as an int. So integer division is used. After that the result is converted to a float.
In general C# will read all sequences (without decimal dot) of digits as an int. An int will be converted to a float if necessary. But before the assignment, that's not necessary. So all calculations in between are done as ints.
In other words, what you've written is:
float A = (float) ((int) 1)/((int) 90)
(made it explicit here, this is more or less what the compiler reads).
Now a division of two int's is processed such that it takes only the integral part into account. The integral part of 0.011111 is 0 thus zero.
If you however modify one of the literals to a floating point (1f, 1.0f, 90f,...) or both, this will work. Thus use one of these:
float A = 1/90.0f;
float A = 1.0f/90;
float A = 1.0f/90.0f;
In that case, floating point division will be performed. Which takes into account both parts.
etc.

Why 2.0 % 0.1 gives 0.09999999 on debugging How to make the solution if i have to do solution if(doubleVar ==0) [duplicate]

This question already has answers here:
Why is floating point arithmetic in C# imprecise?
(3 answers)
Closed 8 years ago.
I am trying to find out modulo in c# as i know remainder is obtained on doing a modulo b= remainder so here a%b=remainder the same i tried to do like this:
var distanceFactor = slider.Value % distance;
But the value on debugging of slider.Value= 2.0 and distance =0.1 and distanceFactor i found surprisingly is 0.0999999999.. and i was expecting it to be 0.
Is it due to var ? what could be the reason for this non zero value.?
And how to do the solution of this problem ? because on rounding of this 0.0999999999 becomes 0.1 ans my control never go in condition if(distanceFactor==0) (and roundoff is also necessary in current situation).Is there any alternative to achieve it ?
This is expected behavior. A floating point number does not exactly represent a decimal number like the type decimal would do. Look at What Every Computer Scientist Should Know About Floating-Point Arithmetic for a detailed description.

What is Double? (C#) [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the difference between Decimal, Float and Double in C#?
Today I'm wondering about Double in .Net. I've used it with Int32 the past days and started wondering what the max value is.
The MSDN page for Double.MaxValue says 1.7976931348623157E+308. I'm pretty sure I'm reading that wrong.
How many bytes does Double take up (in memory)?
What is the actual maximum number (explain the the E+308)?
Is Double.MaxValue bigger than UInt32? Bigger than UInt64?
And while we are at it, what is the difference between Float and Double?
Basically,
Double is 64 bit floating point value and float is a 32 bit.
So double is able to store twice big value as of float.
http://msdn.microsoft.com/en-us/library/678hzkk9(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/b1e65aza(v=vs.71).aspx
Just read the top lines on the links, you'll get an idea.
About E+308: though 2^64 is far less that 1e+308, you must consider that double is not "precise" number, it has only a few significant digits (precision), so it does not need to store all ~308 digits. With this logic behind the double structure, it can contain numbers up to e+308 in 64 bits.

Categories

Resources