开发者

How to specify exponential constants in C#?

开发者 https://www.devze.com 2023-01-28 21:38 出处:网络
I\'ve got some C code that defines a exponential number as a constant. How do I write this in C#? 开发者_开发技巧double TOL = 1.E-8d;

I've got some C code that defines a exponential number as a constant. How do I write this in C#?

开发者_开发技巧    double TOL = 1.E-8d;
    double TOL2 = 1.E - 8;


If there are no numbers after the decimal, you don't include the point. Same as in C/C++/etc. So:

double TOL= 1E-8;
double TOL2 = 1E-8;

Or maybe, for a different value:

double TOL = 1.5E-8;

This is in the spec, section 2.4.4.3:

http://msdn.microsoft.com/en-us/library/ms228593.aspx


    double tol = 1.0e8;
    double tol2 = 1.0e-8;


You were very close with your first form - but you just needed a digit after the ".", or remove the "." entirely:

double TOL = 1.0E-8d;
double TOL = 1E-8d;

See section 2.4.4.3 of the C# language spec for the rules around this. Note that you can use a lower-case "e" if you want, too:

double TOL = 1.0e-8d;
double TOL = 1e-8d;

And double is the default type if you omit the suffix from a "real" literal, so these are valid too:

double TOL = 1.0e-8;
double TOL = 1e-8;

... but personally I'd include the suffix for readability.


This is how you do it in C#:

double value = -4.42330604244772E-305;

See also MSDN on System.Double.


This is slightly different example, and not for exponential numbers.

double value = 1744056d is reported as error by g++ on Linux.

double value = 1744056.0d is fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消