簡體   English   中英

Math.Pow(10,1.946)返回0

[英]Math.Pow(10,1.946) returns 0

我使用以下代碼:

float test = (float)Math.Pow(10,1.946);

問題是此代碼返回0而不是88.30799004 此外,當使用以下代碼時,它將返回0

double test = Math.Pow(10,1.946);

我正在使用Xamarin,我在變量上設置了一個斷點。 使用完全相同的代碼它會消失,但返回0 ,為什么會這樣?

“沒有使用Debug.Write()並跨過斷點:它保持為0.當我添加Debug.Write()並跨過斷點時,它確實返回了正確的值88.30799004。這有點奇怪嗎?”

沒有你想象的那么奇怪。 “Just in Time”編譯器或JiT的一項工作是刪除死代碼。 雖然在調試版本中通常將例程轉換為“非常小的優化”,但仍有一些例程。 我曾經制作此代碼以強制運行時運行到“2 GiB”限制。 沒有任何事情發生,直到我實際上添加了一個輸出:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OOM_32_forced
{
    class Program
    {
        static void Main(string[] args)
        {
            //each short is 2 byte big, Int32.MaxValue is 2^31.
            //So this will require a bit above 2^32 byte, or 2 GiB
            short[] Array = new short[Int32.MaxValue];

            /*need to actually access that array
            Otherwise JIT compiler and optimisations will just skip
            the array definition and creation */
            foreach (short value in Array)
                Console.WriteLine(value);
        }
    }
}

請注意,通常它是一個相當不錯且工作良好的部分。 但遺憾的是,對於極簡主義的測試示例,它很容易導致此類問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM