Unity ã® C# ã§éããªãå°ãã float å¤ã使ãããæã«ã¯ float.Epsilon ã使ã£ã¦ã¯ãã¡
Facebook ã® Unity ã¦ã¼ã¶ã¼å©ãåãæã«ã¦ã 0ã1 ã®ç¯å²ã§ 0 㨠1 ã¯å«ã¾ãªãã¨ãæ¡ä»¶ã§ float åã®ä¹±æ°ãåå¾ããæ¹æ³ã«ã¤ãã¦ã®è³ªåãåºã¦ãã¾ããã
Unityã¦ã¼ã¶ã¼å©ãåãæ
https://www.facebook.com/groups/unityuserj/permalink/821259101267365/
ããã§åºãåçã®ä¸ã®ããã°ã©ã ã®ä¸ã¤ããã¡ãã
Random.Range ( 0f+float.Epsilon, 1f-float.Epsilon );
èªåããã®ããã°ã©ã ãæãã¤ããã®ã§ããèªåã観ãæç¹ã§ã¯æ¢ã«ãã®æ¸ãæ¹ã«ã¯åé¡æãã¨ã®è¿½å ã®ã³ã¡ã³ããä»ãã¦ãã¾ããã
å ·ä½çã«ã¯ 1f-float.Epsilon == 1f ã«æã£ã¦ãã¾ãã¨ãããã®ã§ããã
if æã工夫ãã¦åé¿ãã¦ã¿ã
ãããªã if æã®æ¹ã工夫ããã°è¯ãã®ã§ã¯ï¼ã¨æã£ã¦ä»¥ä¸ã®æ§ãªããã°ã©ã ã§æ¤è¨¼ãã¦ã¿ã¾ããã
float v = 1f - float.Epsilon; if (Mathf.Abs(v - 1f) < float.Epsilon) { Debug.Log ("one"); } else { Debug.Log ("not one"); }
if æã®é¨åã¯ä»¥ä¸ã®æ§ãªå 容ã®ã¯ããªã®ã§ else å´ã«ãªãã "not one" ãåºåãããã¨äºæ³ãã¦ããã®ã§ãã "one" ãåºåããã¾ããã
if (Mathf.Abs(v - 1f) < float.Epsilon) {
ãããâ
if (Mathf.Abs(-float.Epsilon) < float.Epsilon) {
ãããâ
if (float.Epsilon < float.Epsilon) {
äºæ³å¤ã®çµæã¨ãªã£ãã®ã§æ¤è¨¼ã®çºã«ä»¥ä¸ã®ããã°ã©ã ãå®è¡
float v; v = 1f; Debug.Log (v.ToString("G17")); v = 1f - float.Epsilon; Debug.Log (v.ToString("G17")); v = 1f + float.Epsilon; Debug.Log (v.ToString("G17"));
çµæã¯3ã¤ã¨ã 1 ãåºåã
ãªãã° 0 ã®å ´åã¯ã¨ä»¥ä¸ã®ããã°ã©ã ãå®è¡
float v; v = 0f; Debug.Log (v.ToString("G17")); v = 0f - float.Epsilon; Debug.Log (v.ToString("G17")); v = 0f + float.Epsilon; Debug.Log (v.ToString("G17"));
çµæ㯠0 㨠-1.40129846E-45 㨠1.40129846E-45 ã 0 ã«å ããå ´åã«ã¯ã¡ããã¨åæ ãããæ§ã§ããæµ®åå°æ°ç¹æ°ã®ã丸ã誤差ãã®å½±é¿ã§ 0 ã«å ããå ´åã¯åæ ããããããã© 1 ã«å ããå ´åã«ã¯åæ ããã¥ãã(丸ãããã)ã®ããªï¼ã§ã float.Epsilon ãªã®ã«ä½æ ï¼
.NET ã®è¨ç®æ©ã¤ãã·ãã³
Wikipedia の「計算機イプシロン」ã®ã.NETãã«ã¤ãã¦ã®é ç®ã確èªããã¨æãããè¨è¿°ã
Microsoftã®.NET Frameworkã®ã©ã¤ãã©ãªã«ãDouble.Epsilonã¨ãããã£ã¼ã«ãï¼ããããã£ï¼ãããããããã¯æµ®åå°æ°ç¹æ¹å¼ã§è¡¨ç¾å¯è½ãªæå°ã®æ£ã®éæ£è¦åæ°ã§ãããè¨ç®æ©ã¤ãã·ãã³ã§ã¯ãªãã
è¨ç®æ©ã¤ãã·ãã³ã§ã¯ãªã!! è¨ç®æ©ã¤ãã·ãã³ã§ã¯ãªã!!
èªåã§ã¤ãã·ãã³ã®å¤ãæ±ãã
ã¨ããäºã§ã Unity 㯠Mono ã ãã© .NET ç¸å½ã ãããã®è¾ºãã®ã«ã¼ã«ãåãã ããã¨èãã㨠float.Epsilon ã使ãã¨ããã¢ã¤ãã£ã¢ã¯ãã¡ã§ããã
代ããã«èªåã§ãè¨ç®æ©ã¤ãã·ãã³ãã®å¤ãæ±ãã¾ãããã
ä»åã¯ã¢ã«ã´ãªãºã æ¬ã¨ãã¦æåãªå¥¥ææ´å½¦ããã®『コンピュータ・アルゴリズム事典』(技術評論社,1991年)のサポートページã«ã¦é
å¸ããã¦ããソースコードã®ä¸ã® float.c ãåèã«ä»¥ä¸ã®æ§ãªããã°ã©ã ãä½ã£ã¦ã¿ã¾ããã
using UnityEngine; using System.Collections; public class Test : MonoBehaviour { float foo(float x) { return x; } void Start() { int b, p; float x, y, eps; x = y = 2; while (foo(x + 1) - x == 1) x *= 2; while (foo(x + y) == x) y *= 2; b = (int)(foo(x + y) - x); p = 1; x = b; while (foo(x + 1) - x == 1) { p++; x *= b; } eps = 1; while (foo(1 + eps / 2) > 1) eps /= 2; eps = foo(1 + eps) - 1; Debug.Log(string.Format("b = {0}, p = {1}, eps = {2}\n", b, p, eps)); float t; // Test1 t = 0; Debug.Log(string.Format("0 + eps = {0:G17}\n", t + eps)); // Test2 t = 1; Debug.Log(string.Format("1 - eps = {0:G17}\n", t - eps)); // Test3 t = 0; if (0 == (t + eps)) { Debug.Log("equal"); } else { Debug.Log("not equal"); } Debug.Log(string.Format("1 / (0 + eps) = {0}\n", 1/(t + eps))); } }
ãã®ããã°ã©ã ã®å®è¡çµæã¯ãã¡ãã
b = 2, p = 24, eps = 1.192093E-07 0 + eps = 1.192093E-07 1 - eps = 0.999999881 not equal 1 / (0 + eps) = 8388608
ãã¹ã¦è¯ãæãã®çµæãè¿ã£ã¦ãã¦ãã¾ãã
ã¨ããäºã§ Unity ã§ã¯ããããå ´é¢ã§ã¯ float.Epsilon ã®ä»£ããã« 1.192093E-07 (â»)ã使ãã¨è¯ãã§ãããã
â»ããã¾ã§ Mac ã§å®è¡ããæã«ç®åºããå¤ãªã®ã§ OS ã CPU ã®éãã«ãã£ã¦ã¯ç°ãªãå¤ã«ãªãå¯è½æ§ãæããã¨ã«ã¯æ³¨æãã¦ä½¿ã£ã¦ä¸ããã
å ã¿ã« float.Epsilon ã Mathf.Epsilon ã«è¨å®ããã¦ããå¤ã¯ 1.40129846E-45 ã§ããã
é¢é£æ¸ç±
Cè¨èªã«ããææ°ã¢ã«ã´ãªãºã äºå ¸ (ã½ããã¦ã§ã¢ãã¯ããã¸ã¼)
- ä½è : 奥ææ´å½¦
- åºç社/ã¡ã¼ã«ã¼: æè¡è©è«ç¤¾
- çºå£²æ¥: 1991/03/01
- ã¡ãã£ã¢: åè¡æ¬
- è³¼å ¥: 20人 ã¯ãªãã¯: 396å
- ãã®ååãå«ãããã° (95件) ãè¦ã
åèãµã¤ã
å°æ°ï¼æµ®åå°æ°ç¹æ°åï¼ã®è¨ç®ãæã£ãçµæã«ãªããªãçç±ã¨è§£æ±ºæ³: .NET Tips: C#, VB.NET
http://dobon.net/vb/dotnet/beginner/floatingpointerror.html