2008-12-07 30 views
55

Floatlar kullanarak bazı C kodları ile uğraşıyorum ve ekranda float yazdırmaya çalıştığımda 1. # INF00, -1. # IND00 ve -1. # IND alıyorum. Bu değerler ne anlama geliyor?1. # INF00, -1. # IND00 ve -1. # IND ne anlama geliyor?

1. # INF00'ün pozitif sonsuzluk anlamına geldiğine inanıyorum, ama peki -1. # IND00 ve -1. # IND? Bazen bu değeri de gördüm: 1. Numara Değil NaN, ama bu garip değerlere ne sebep oluyor ve bunlar bana hata ayıklama konusunda nasıl yardımcı olabilir?

MinGW kullanıyorum, float point sayıları için IEEE 754 gösterim kullanın.

Birisi tüm bu geçersiz değerleri ve ne anlama geldiğini listeleyebilir mi? IEEE floating-point exceptions in C++ itibaren

cevap

61

: Aşağıdaki olmayan numaralar filtrelemek için kullanışlı bir yol olabilir bir .NET ortamında olanlar için

This page will answer the following questions.

  • My program just printed out 1.#IND or 1.#INF (on Windows) or nan or inf (on Linux). What happened?
  • How can I tell if a number is really a number and not a NaN or an infinity?
  • How can I find out more details at runtime about kinds of NaNs and infinities?
  • Do you have any sample code to show how this works?
  • Where can I learn more?

These questions have to do with floating point exceptions. If you get some strange non-numeric output where you're expecting a number, you've either exceeded the finite limits of floating point arithmetic or you've asked for some result that is undefined. To keep things simple, I'll stick to working with the double floating point type. Similar remarks hold for float types.

Debugging 1.#IND, 1.#INF, nan, and inf

If your operation would generate a larger positive number than could be stored in a double, the operation will return 1.#INF on Windows or inf on Linux. Similarly your code will return -1.#INF or -inf if the result would be a negative number too large to store in a double. Dividing a positive number by zero produces a positive infinity and dividing a negative number by zero produces a negative infinity. Example code at the end of this page will demonstrate some operations that produce infinities.

Some operations don't make mathematical sense, such as taking the square root of a negative number. (Yes, this operation makes sense in the context of complex numbers, but a double represents a real number and so there is no double to represent the result.) The same is true for logarithms of negative numbers. Both sqrt(-1.0) and log(-1.0) would return a NaN, the generic term for a "number" that is "not a number". Windows displays a NaN as -1.#IND ("IND" for "indeterminate") while Linux displays nan. Other operations that would return a NaN include 0/0, 0*∞, and ∞/∞. See the sample code below for examples.

In short, if you get 1.#INF or inf, look for overflow or division by zero. If you get 1.#IND or nan, look for illegal operations. Maybe you simply have a bug. If it's more subtle and you have something that is difficult to compute, see Avoiding Overflow, Underflow, and Loss of Precision. That article gives tricks for computing results that have intermediate steps overflow if computed directly.

+3

OP'nin bunu gerçekten istemediğini biliyorum, ancak kullanışlı bir test olarak, bu sihirli değerlerden birine sahipseniz, “myfloat == myfloat” false değerini döndürecektir. – tenpn

+7

@tenpn Aslında C++, + sonsuzluk == + sonsuzluk. 1.0/0.0 'ı kontrol etmeyi deneyin: '1. # INF00' ==' 1. # INF00', true döndürür, -1-INF00' == '-1. INF00', true değerini döndürür, ancak' 1. INF00' = = '-1. # INF00' yanlış. – bobobobo

+2

Diğer yapılandırmalar hakkında emin değilim, ancak Windows 7/Visual Studio 2010'da. float nan = sqrtf (-1.0f); nan == nan; // true olarak değerlendirir ... tenpn'in söylediklerinin tersine .. (Yevgen V'e göre) – Jeff

3

(bu örnek VB.NET içinde, ama muhtemelen benzer C#):

If Double.IsNaN(MyVariableName) Then 
    MyVariableName = 0 ' Or whatever you want to do here to "correct" the situation 
End If 

Eğer aşağıdaki hatayı alırsınız NaN değeri vardır bir değişken kullanmayı denerseniz:

Value was either too large or too small for a Decimal.

+3

Bu algılanmıyor '1. # INF'. Ayrıca +/- sonsuzluğunu kontrol etmek için 'Double.IsInfinity (MyVariableName)' kullanmanız gerekir. – user1318499

2

Sorunuz "neyin var?" Yukarıda zaten yanıtladı.

Bildiğim kadarıyla ayıklama (sizin ikinci soru) olsa ve özel giriş değerleri için kontrol etmek istediğiniz kütüphaneler gelişmekte, sen ++ Windows'un C kullanışlı aşağıdaki işlevleri bulabilirsiniz gibidir:

_isnan(), _isfinite () ve _fpclass()

Linux/Unix'te isnan(), isfinite(), isnormal(), isinf(), fpclassify() öğelerini bulmalısınız (ve derleyiciyi kullanarak libm ile bağlantı kurmanız gerekebilir) bayrak -lm).