【C#】異なる型同士での加減乗除について
今回は異なる型同士での加減乗除について記述します。
学習している中、ふと思いました。
「int型 × double型って何型になるの?もしかしてニュータイプ生まれる!?」
小さな期待とこれは必要なことか?という疑問を抱きつつ調べることにしました。
ただの興味本位ですのであしからず。。。
using System; class Task_4 { public static void Main() { sbyte a = 1; short b = 1; int c = 1; long d = 1; byte e = 1; ushort f = 1; uint g = 1; ulong h = 1; float i = 1; double j = 1; dynamic k = 1; // sbyte dynamic ab = a * b; dynamic ac = a * c; dynamic ad = a * d; dynamic ae = a * e; dynamic af = a * f; dynamic ag = a * f; dynamic ai = a * i; dynamic aj = a * j; dynamic ak = a * k; //short dynamic bc = b * c; dynamic bd = b * d; dynamic be = b * e; dynamic bf = b * f; dynamic bg = b * g; dynamic bi = b * i; dynamic bj = b * j; dynamic bk = b * k; //int dynamic cd = c * d; dynamic ce = c * e; dynamic cf = c * f; dynamic cg = c * g; dynamic ci = c * i; dynamic cj = c * j; dynamic ck = c * k; //long dynamic de = d * e; dynamic df = d * f; dynamic dg = d * g; dynamic di = d * i; dynamic dj = d * j; dynamic dk = d * k; //byte dynamic ef = e * f; dynamic eg = e * g; dynamic eh = e * h; dynamic ei = e * i; dynamic ej = e * j; dynamic ek = e * k; //ushort dynamic fg = f * g; dynamic fh = f * h; dynamic fi = f * i; dynamic fj = f * j; dynamic fk = f * k; //uint dynamic gh = g * h; dynamic gi = g * i; dynamic gj = g * j; dynamic gk = g * k; //ulong dynamic hi = h * i; dynamic hj = h * j; //dynamic hk = h * k; //float dynamic ij = i * j; dynamic ik = i * k; //double dynamic jk = j * k; //sbyte * ... Console.WriteLine("▼ sbyte *"); Console.WriteLine("*=--------------------------------------=*"); Console.WriteLine("sbyte * short … " + ab.GetType()); Console.WriteLine("sbyte * int … " + ac.GetType()); Console.WriteLine("sbyte * long … " + ad.GetType()); Console.WriteLine("sbyte * byte … " + ae.GetType()); Console.WriteLine("sbyte * ushort … " + af.GetType()); Console.WriteLine("sbyte * uint … " + ag.GetType()); Console.WriteLine("sbyte * float … " + ai.GetType()); Console.WriteLine("sbyte * double … " + aj.GetType()); Console.WriteLine("sbyte * dynamic … " + ak.GetType()); //short * ... Console.WriteLine("\n▼ short *"); Console.WriteLine("*=--------------------------------------=*"); Console.WriteLine("short * int … " + bc.GetType()); Console.WriteLine("short * long … " + bd.GetType()); Console.WriteLine("short * byte … " + be.GetType()); Console.WriteLine("short * ushort … " + bf.GetType()); Console.WriteLine("short * uint … " + bg.GetType()); Console.WriteLine("short * float … " + bi.GetType()); Console.WriteLine("short * double … " + bj.GetType()); Console.WriteLine("short * dynamic … " + bk.GetType()); //int * ... Console.WriteLine("\n▼ int *"); Console.WriteLine("*=--------------------------------------=*"); Console.WriteLine("int * long … " + cd.GetType()); Console.WriteLine("int * byte … " + ce.GetType()); Console.WriteLine("int * ushort … " + cf.GetType()); Console.WriteLine("int * uint … " + cg.GetType()); Console.WriteLine("int * float … " + ci.GetType()); Console.WriteLine("int * double … " + cj.GetType()); Console.WriteLine("int * dynamic … " + ck.GetType()); //long * ... Console.WriteLine("\n▼ long *"); Console.WriteLine("*=--------------------------------------=*"); Console.WriteLine("long * byte … " + de.GetType()); Console.WriteLine("long * ushort … " + df.GetType()); Console.WriteLine("long * uint … " + dg.GetType()); Console.WriteLine("long * float … " + di.GetType()); Console.WriteLine("long * double … " + dj.GetType()); Console.WriteLine("long * dynamic … " + dk.GetType()); //byte * ... Console.WriteLine("\n▼ byte *"); Console.WriteLine("*=--------------------------------------=*"); Console.WriteLine("byte * ushort … " + ef.GetType()); Console.WriteLine("byte * uint … " + eg.GetType()); Console.WriteLine("byte * ulong … " + eh.GetType()); Console.WriteLine("byte * float … " + ei.GetType()); Console.WriteLine("byte * double … " + ej.GetType()); Console.WriteLine("byte * dynamic … " + ek.GetType()); //ushort * ... Console.WriteLine("\n▼ushort *"); Console.WriteLine("*=--------------------------------------=*"); Console.WriteLine("ushort * uint … " + fg.GetType()); Console.WriteLine("ushort * ulong … " + fh.GetType()); Console.WriteLine("ushort * float … " + fi.GetType()); Console.WriteLine("ushort * double … " + fj.GetType()); Console.WriteLine("ushort * dynamic … " + fk.GetType()); //uint * ... Console.WriteLine("\n▼ uint *"); Console.WriteLine("*=--------------------------------------=*"); Console.WriteLine("uint * ulong … " + gh.GetType()); Console.WriteLine("uint * float … " + gi.GetType()); Console.WriteLine("uint * double … " + gj.GetType()); Console.WriteLine("uint * dynamic … " + gk.GetType()); //ulong * ... Console.WriteLine("\n▼ ulong *"); Console.WriteLine("*=--------------------------------------=*"); Console.WriteLine("ulong * float … " + hi.GetType()); Console.WriteLine("ulong * double … " + hj.GetType()); //float * ... Console.WriteLine("\n▼ float *"); Console.WriteLine("*=--------------------------------------=*"); Console.WriteLine("float * double … " + ij.GetType()); Console.WriteLine("float * dynamic … " + ik.GetType()); //double * ... Console.WriteLine("\n▼ double *"); Console.WriteLine("*=--------------------------------------=*"); Console.WriteLine("double * dynamic … " + jk.GetType()); } }
◆加減乗除の実行結果
◆備考
基本的には大きい型の方に変換されることが分かりました。
また、ulongと加減乗除するとエラーになるパターンがありました。
・ulong * sbyte
・ulong * short
・ulong * int
・ulong * long
・ulong * dynamic
ulongは下限値が0のため下限値がマイナスの型と計算するとエラーになるのか????
しかし、浮動小数点型もマイナスを含むけどエラーにならない。。。
ただただ謎が深まっただけでした。。。
以上です。
この記事が誰かの役に立つことを祈ります。