Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
float to short cast overflow
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { static float DoSomething1(float val) { return Math.Max(Int16.MinValue, Math.Min(Int16.MaxValue, val * (val > 0 ? Int16.MaxValue : -Int16.MinValue))); } static float DoSomething2(float val) { if (val >= 1.0f) return Int16.MaxValue; if (val <= -1.0f) return Int16.MinValue; val = val > 0 ? val * Int16.MaxValue : -val * Int16.MinValue; return (float) Math.Round(val); } static void DoThings(float stick) { string fmt = ": {0};-{0}"; string fmt1 = String.Format(fmt, "0.00"); string fmt2 = String.Format(fmt, "00000.00"); float ohboy0 = DoSomething1(stick); // Works // float ohboy0 = DoSomething2(stick); // Works float ohboy1 = -1 * ohboy0; // Fails // float ohboy1 = DoSomething1(-1 * stick); // Works // float ohboy1 = DoSomething2(-1 * stick); // Works short ohmy0 = (short)ohboy0; short ohmy1 = (short)ohboy1; bool annieareyouokay = (int)ohboy0 == (int)ohmy0 && (int)ohboy1 == (int)ohmy1; Console.WriteLine("{0" + fmt1 + "} -> ({1" + fmt2 + "}, {2" + fmt2 + "}) -> ({3" + fmt2 + "}, {4" + fmt2 + "}) ? {5}", stick, ohboy0, ohboy1, ohmy0, ohmy1, annieareyouokay); } public static void Main(string[] args) { Console.WriteLine("{0} ... {1}", Int16.MinValue, Int16.MaxValue); Console.WriteLine("{0} ... {1}", -Int16.MaxValue, -Int16.MinValue); // Note the implicit cast short min_val = Int16.MinValue; Console.WriteLine("({0}) {1} == ({2}) {3} ? {4}", min_val.GetType(), min_val, (-min_val).GetType(), -min_val, min_val == -min_val); // Force the type to remain short short minus_min_val; // Compiles minus_min_val = (short)-min_val; // unchecked { minus_min_val = (short)-Int16.MinValue; } // Does not compile // minus_min_val = -min_val; // minus_min_val = -Int16.MinValue; // minus_min_val = (short)-Int16.MinValue; // unchecked { minus_min_val = -min_val; } // unchecked { minus_min_val = -Int16.MinValue; } Console.WriteLine("({0}) {1} == ({2}) {3} ? {4}", min_val.GetType(), min_val, minus_min_val.GetType(), minus_min_val, min_val == minus_min_val); Console.WriteLine(); for (int i = 95; i < 105; i++) { DoThings(i / 100.0f); DoThings(-i / 100.0f); } } } }
run
|
edit
|
history
|
help
1
Linear search
147
Jennifer Lawrence hot season Live cam
Project Euler Problem 11
Coding Exercise
25
בדיקה כמה פעמים מערך אחד כלול במערך אחר
Hex<->Bas64
30272 Program Ex4 if
axasd