Mathematical classes in C #, math, floating-point numbers (middle)

Source: Internet
Author: User
Tags arithmetic
mathematical classes in C #, math, floating-point numbers (middle) Preface

Today would not want to write, one is written quite a lot, and then write also remember, but want to go back to the early morning to sleep, tomorrow to learn the car, but this point back to sleep a bit early ah, then write the last one I went back!

Body

And then there's nothing to say, two-week floating-point numbers: float and double are defined in accordance with the Ansi/ieee std754-1985 (which is an IEEE standard for binary floating-point arithmetic).

The float value consists of a 24-bit signed Mantissa and a 8-bit signed exponent. The precision is seven digits after the decimal point. Minimum float value worthy of range and greater than 0:

Float. Minvalue,float. Maxvalue,float. Epsilon

Try to output it yourself to see what the result is.

The double value consists of a 53-bit signed Mantissa and a 11-bit signed exponent with a precision of approximately 16 digits after the decimal point. The range of values and the minimum double value greater than 0:

Double. Minvalue,double. Maxvalue,double. Epsilon

The following code is a floating-point number divided by 0:

float f1=1;

float f2=0;

float f3=f1/f3;

When you see the code above, you may suddenly think of an exception: Dividedbyzeroexception. If these numbers are integers now, they do, but! These are floating-point numbers and do not produce an exception.

In fact, floating-point arithmetic never produces an exception, because F2 is not really 0, it's just a very close to 0 number (how many?). In this case, the F3 equals a special value. It is infinity (positive infinity).

If you change F1 to -1,F3, the result is-infinity (negative infinity).

You can even perform arithmetic operations on infinity, for example, an expression 1/f3 equals 0.

If you change the F1 to 0, that is, 0/infinity, then F3 equals Nan (not a number).

To determine whether a number is infinity or Nan uses a single static method:

BOOL Isinfinity (float fvalue);                      Determine if the infinity is a
bool Ispositiveinfinity (float fvalue);           Judge whether is positive infinity large
bool Isnegativeinfinty (float fvalue);          To determine whether the negative infinity large
bool isNaN (float fvalue);                          To determine whether Nan


The single structure also defines some constant fields

Single.PositiveInfinity.

Single.NegativeInfinity.

Single.nan.

Use C # code output to see

            Console.WriteLine (single.positiveinfinity);
            Console.WriteLine (single.negativeinfinity);
            Console.WriteLine (Single.nan);



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.