5

I saw we can get the value of PI thanks to Math.PI in C#.

Do you know if exists a library or a package for getting rounded values for other famous constants, like for example, Avogadroo, Kelvin, Planck, Coulombs, golden number, Newton constant?

It would be very greate if we could do some simple convertions.

In other world: a light moment.js for sciences, it would save a lot of time to a lot of people.

lambdaDev
  • 500
  • 4
  • 18
  • 1
    By the way, `Math.PI` isn't `π`. It's `3.14159265358979323846`. – Thomas Ayoub Feb 03 '16 at 09:29
  • 1
    Why does this question have so many upvotes? It is a really low quality one that does not show ANY research afford. – MakePeaceGreatAgain Feb 03 '16 at 09:30
  • certainly it is a feature that a lot of people would like to have but don't dare to ask if it exists on this forum cause they are affraid to have downvotes by people like you – lambdaDev Feb 03 '16 at 09:49
  • @HimBromBeere wanna see many upvotes on question that *does not show ANY research afford*? See [Switch in Ruby](http://stackoverflow.com/questions/948135/how-can-i-write-a-switch-statement-in-ruby) – Thomas Ayoub Feb 03 '16 at 09:57
  • @Thomas Outch, yes you´re right. However it´s quite annoying. – MakePeaceGreatAgain Feb 03 '16 at 10:00
  • @Thomas @ HimBromBeere I searched and found the article before to ask on forum, but I hope it exists a more useful way than write my own class. Maybe it exists a package , a library you don't know and nobody asked the question before on that useful forum. I am not the only person who is expecting a more useful answere – lambdaDev Feb 03 '16 at 10:00
  • What exactly is your problem on writing your own class using Rahuls approach? Then you are indeoendent on external sources which might be a great thing depending on the restrictions in your company or organization. Anyway looking for libraries and tools is offtopic for SO and thus the question may get closed. – MakePeaceGreatAgain Feb 03 '16 at 10:03
  • @Thomas (your first comment) The number you quote is closer to the true π than a `double` can be. It appears that `Math.PI` is a `double` equivalent to `7074237752028440 / 2**51`. Since the numerator is divisible by eight, that is `884279719003555 / 2**48`. The decimal expansion starts with `3.14159265358979311599796346854418516`. And `Math.PI.ToString("G17", CultureInfo.InvariantCulture)` gives `"3.1415926535897931"`. – Jeppe Stig Nielsen Feb 03 '16 at 10:06
  • @HimBromBeere it is not a problem but for exemple I like the library momentJs that do things I could do by my own. Maybe it could exists a package that let me choose in params several solutions or different truncations – lambdaDev Feb 03 '16 at 10:06
  • @JeppeStigNielsen I let you tell that to Microsoft guys since it's [what they wrote](http://referencesource.microsoft.com/#mscorlib/system/math.cs,37) – Thomas Ayoub Feb 03 '16 at 10:09
  • @Thomas Oh, Microsoft just took pi with a lot of extra digits and put it in their source code, and that is fine. They can be sure to get the best `double` possible, so it is the best they can do. However, not all digits are actually used when you write a "long" `double` literal like that. – Jeppe Stig Nielsen Feb 03 '16 at 10:16

2 Answers2

4

As Him has already mentioned you can use const keyword for storing the constant values. You can also see this article:

// Physical Constants in cgs Units

// Boltzman Constant. Units erg/deg(K) 
public const double BOLTZMAN = 1.3807e-16;

// Elementary Charge. Units statcoulomb 
public const double ECHARGE = 4.8032e-10;

// Electron Mass. Units g 
public const double EMASS = 9.1095e-28;

// Proton Mass. Units g 
public const double PMASS = 1.6726e-24;

// Gravitational Constant. Units dyne-cm^2/g^2
public const double GRAV = 6.6720e-08;

// Planck constant. Units erg-sec 
public const double PLANCK = 6.6262e-27;

// Speed of Light in a Vacuum. Units cm/sec 
public const double LIGHTSPEED = 2.9979e10;

// Stefan-Boltzman Constant. Units erg/cm^2-sec-deg^4 
public const double STEFANBOLTZ = 5.6703e-5;

// Avogadro Number. Units  1/mol 
public const double AVOGADRO = 6.0220e23;

// Gas Constant. Units erg/deg-mol 
public const double GASCONSTANT = 8.3144e07;

// Gravitational Acceleration at the Earths surface. Units cm/sec^2 
public const double GRAVACC = 980.67;

// Solar Mass. Units g 
public const double SOLARMASS = 1.99e33;

// Solar Radius. Units cm
public const double SOLARRADIUS = 6.96e10;

// Solar Luminosity. Units erg/sec
public const double SOLARLUM = 3.90e33;

// Solar Flux. Units erg/cm^2-sec
public const double SOLARFLUX = 6.41e10;

// Astronomical Unit (radius of the Earth's orbit). Units cm
public const double AU = 1.50e13;

It would be great if it would exists a a package, a library or a native class for that

As such there is no such library which you can use or a native class. The best is to include the above constants in a separate class and then use it.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • 2
    More precise values, and in SI units (if preferred) can be found on [http://physics.nist.gov/cuu/Constants/](http://physics.nist.gov/cuu/Constants/). For example, the Boltzmann constant (mis-spelled above) is `1.38064852e-23` in joules pr. kelvin. Uncertainty is given on that page. – Jeppe Stig Nielsen Feb 03 '16 at 09:25
  • 1
    it is a good answere, thank for posted it. but it would be great and still hope to have a package or a library, if I don't have tomorow I will mark it as answere – lambdaDev Feb 03 '16 at 09:56
1

You can use the const-keyword for this:

class MyClass {
    public const double KELVIN = -273.15;
}

Now access it using MyClass.KELVIN.

However depending on the accuracy you need for your calculations you may need different datatypes, e.g. decimal, float or double.

There does not exist a library for those constants because those constantns share very different aspects and noone so far as considered them to be contained in one library. However you may do it by adding them to a class and publish it on GitHub.

Community
  • 1
  • 1
MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111