Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Yes, I realize decimal or float has been asked before, and I know their uses and that decimal is precise and float is non-accurate.
In a game I'm working on, I have the player, which has a movement speed of 36 pixel movement per second. I update this about 10 times a second, making it 3.6 each time. I want this to be precise, but decimal takes up 16 bytes (128 bits), which is a lot. If I want to keep the accuracy, should I use integer instead and scale the numbers? Or.. Is float more suitable? Float and Int32 both take up 4 bytes, but an integer doesn't loose any numbers. So shouldn't Integer be more suitable? Then why would anyone ever use float?
So basically, which one should I use for speed and accuracy? I'm using decimal at the moment. I want to change it before I get into too much detail in the game, or it's going to get harder to make all those changes.
16 Bytes is a lot compared to other types. Go with what satisfies your requirement, no premature optimization.
You might also consider using int and interpreting it as x/10, that way the inaccuracy won't accumulate. But only if those 128bits really bug you (which they shouldn't, imho).
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have an array of numbers up to 20000 and I'm trying to assign a weight to these numbers:
The closer a number is to 0 the higher should the weight be. My problem is that I'm trying to make it such that the higher the number is, the smaller should the difference in weight be, for example the weight difference between 1-100 might be 1.5 but the difference between 100-10000 might be 0.5.
I think it's a logarithmic scale, isn't it? I'm not great at math at all.. this is not a homework question, school was out long ago just a hobby question.
What I've tried is that I've mapped weights to my number array by doing a square root on 25000-value but this isn't what I'm looking for. I just put that in so I could see a gradient of weights coming back plus the numbers are just to big, ideally I want the weights between 0.01 and 3.
I don't have any code to show, any help would be appreciated.
While your question isn't really a C# question, I may have an answer for you.
To scale a value with logarithmic spacing, you can use the following formula:
You said you maximum value is 20000 and you want to scale the values from 0.01 to a maximum of 3, so we need to insert the max and scale our formula:
// edit: also the values should be reversed, so subtract the log from 1:
This gives the following values f(x) for values of x:
f(0) = 3
f(1) = 2.79
f(10) = 2.27
f(100) = 1.60
f(1000) = 0.91
f(10000) = 0.21
f(20000) = 0
Would that suffice for your case?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I've read a few posts and I understand how double arithmetic doesn't always return the results you would expect.
Why is floating point arithmetic in C# imprecise?
My problem is different though because I'm checking to see if two numbers are the same and I'm getting false when I would expect true.
I've also read this What Every Computer Scientist Should Know About Floating-Point Arithmetic, but am still having trouble understanding why two seemingly equal double variables are showing unequal.
UPDATE: They answer below helped me understand that the value displayed by the debugger wasn't the 'whole story'. That is why the two floats seemed equal. The debugger was showing equal values when hovering the variables.
Default string format is not precise for float numbers, should use "G17"
double a = 17.125 / 3.0;
double b = 17.12499999999999 / 3.0;
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(a.ToString("G17"));
Console.WriteLine(b.ToString("G17"));
Console.WriteLine(a == b);
Console.WriteLine(Convert.ToString(*(long*)&a, 2));
Console.WriteLine(Convert.ToString(*(*)&b, 2));
Result:
5.70833333333333
5.70833333333333
5.708333333333333
5.7083333333333295
False
100000000010110110101010101010101010101010101010101010101010101
100000000010110110101010101010101010101010101010101010101010001
Usually to compare 2 float numbers, you can use a small error number
Console.WriteLine(Math.Abs(a - b) < 0.0000001);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to know what is the size in memory in bytes of this float number for example -0,005606111.
According to the documentation a float takes up 32 bits (4 bytes), a double takes 64 bits (8 byteS) and a decimal takes 128 bytes (16 bytes).
Note, however that a float offers a precision of 7 digits, so it won't be appropriate for storing the value -0.005606111. Instead, you should use a double (15-16 digits of precision) or a decimal (28-29 digits).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I've come across a problem where my score numbers have turn into exponents.
What I want to know is, how do you convert large floats and int exponent numbers into regular long numbers?
This is probably just a matter of the default number formatting when converting ints and floats to string. To specify a particular conversion method, try using string.Format() with the appropriate format string. Here's some info on number format strings for use with string.Format. By default, .NET appears to give you what you'd get with "General." (You probably want "Fixed-point.")
Sample code:
Console.WriteLine(string.Format("my long number is {0:F}", 1234567891234567891.23));
Thanks everyone for the help.
Here is a solution for those who use unity3d C#:
Select a format style from here:
https://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx
Thankyou adv12 for the link
before:
public GUIText score;
void update()
{
score.text = "Score:" + clicks;
}
After:
public GUIText score;
void Update ()
{
score.text = "Score:" + clicks.ToString ("f");
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Can anybody tell me what is the difference between these two functions in C#? TotalDays and Days because I'm not sure which once I should use in my code? Sorry for the low information on this text, but there is not much I can talk about.
Since i haven't found a duplicate i post my comment here:
Always read the documentation first. TotalDays is a double because it represents whole and fractional days whereas Days is an int which represents only whole days.
That is even mentioned explicitly in the remarks sections of TimeSpan.Days/TotalDays:
The Days property represents whole days, whereas the TotalDays
property represents whole and fractional days.
One thing to note, as opposed to the other properties in TimeSpan like Hours/TotalHours there is no limit on Days. So it doesn't end with 30 or 365(like Hour which ranges from -23 through 23) since there is no larger unit than year. So Days will always be the same number as (int) ts.TotalDays.
A TimeSpan doesn't have a sensible concept of "years" because it
depends on the start and end point. (Months is similar - how many
months are there in 29 days? Well, it depends...) [J. Skeet]