My computer can't do basic arithmetic (or I'm doing something stupid)
-
float aspectRatio = 1280 / 1024;
Console.WriteLine(aspectRatio.ToString());RESULT? 1
float aspectRatio = 1280 / 800;
Console.WriteLine(aspectRatio.ToString());RESULT? 1
float aspectRatio = 1280 / 500;
Console.WriteLine(aspectRatio.ToString());RESULT? 2 Any idea why this might be?
-
float aspectRatio = 1280 / 1024;
Console.WriteLine(aspectRatio.ToString());RESULT? 1
float aspectRatio = 1280 / 800;
Console.WriteLine(aspectRatio.ToString());RESULT? 1
float aspectRatio = 1280 / 500;
Console.WriteLine(aspectRatio.ToString());RESULT? 2 Any idea why this might be?
try
float aspectRatio = 1280f / 500f;
what you're doing is calculating with integers and assigning the result to float. you need to calculate with floats.
-
float aspectRatio = 1280 / 1024;
Console.WriteLine(aspectRatio.ToString());RESULT? 1
float aspectRatio = 1280 / 800;
Console.WriteLine(aspectRatio.ToString());RESULT? 1
float aspectRatio = 1280 / 500;
Console.WriteLine(aspectRatio.ToString());RESULT? 2 Any idea why this might be?
The compilers understands the right side expressions to be integers. You only get a decent result with
float aspectRatio = (float)1280 / (float)500;
-
float aspectRatio = 1280 / 1024;
Console.WriteLine(aspectRatio.ToString());RESULT? 1
float aspectRatio = 1280 / 800;
Console.WriteLine(aspectRatio.ToString());RESULT? 1
float aspectRatio = 1280 / 500;
Console.WriteLine(aspectRatio.ToString());RESULT? 2 Any idea why this might be?
I see. Thanks to you both.
-
float aspectRatio = 1280 / 1024;
Console.WriteLine(aspectRatio.ToString());RESULT? 1
float aspectRatio = 1280 / 800;
Console.WriteLine(aspectRatio.ToString());RESULT? 1
float aspectRatio = 1280 / 500;
Console.WriteLine(aspectRatio.ToString());RESULT? 2 Any idea why this might be?
The result of an arithmetic operation on two integers is an integer. To get the float value, you must do explicit casting.
-
float aspectRatio = 1280 / 1024;
Console.WriteLine(aspectRatio.ToString());RESULT? 1
float aspectRatio = 1280 / 800;
Console.WriteLine(aspectRatio.ToString());RESULT? 1
float aspectRatio = 1280 / 500;
Console.WriteLine(aspectRatio.ToString());RESULT? 2 Any idea why this might be?
Hi, You need to type cast the expression to float like the following: float aspectRatio = (float)1280 / 1024; MessageBox.Show(aspectRatio.ToString()); Or float aspectRatio = (float)1280 / 500; MessageBox.Show(aspectRatio.ToString()); This will fix the issue.
Regards, John Adams ComponentOne LLC