3
$\begingroup$

I am trying to obtain the value of physical constants such as speed of light $c = 2.998 \times 10^8\,\rm{m/s}$. But Mathematica doesnt seem to give me a direct answer. I have to do a round-about method using UnitConvert as shown below. How do I get it to express in different forms like engineering, scientific notations like $2.998 \times 10^8\,\rm{m/s}$?

No matter what wrapper I use, I seem to get the same output.

MWE:

UnitConvert[Quantity[1, "SpeedOfLight"],   "meters/sec"] // EngineeringForm
UnitConvert[Quantity[1, "SpeedOfLight"],   "meters/sec"] // TraditionalForm
UnitConvert[Quantity[1, "SpeedOfLight"],   "meters/sec"] // ScientificForm

Output:

299792458m/s
299792458m/s
299792458m/s
$\endgroup$
  • 2
    $\begingroup$ Use numerical values: e.g. N @ UnitConvert[Quantity[1, "SpeedOfLight"], "meters/sec"] // EngineeringForm. With integer input EngineeringForm and ScientificForm give the input back. (Because, for example EngineeringForm prints with all real numbers in expr given in engineering notation .) and TraditionalForm just adds digit blocks in the formatting. $\endgroup$ – kglr Dec 28 '17 at 22:05
  • $\begingroup$ @kglr please see my comment to the accepted answer below. $\endgroup$ – John Honai Dec 29 '17 at 13:54
3
$\begingroup$

kglr comment is one way to solve your problem, but it can also be solved by making sure you give UnitConvert a machine number in its 1st argument.

UnitConvert[Quantity[1., "SpeedOfLight"], "meters/sec"] // EngineeringForm

299.792*10^(6)m/s

UnitConvert[Quantity[1., "SpeedOfLight"], "meters/sec"] // ScientificForm

2.99792*10^(8)m/s

If you are going to do this sort of thing a lot, I recommend writing a custom function -- a very simple one, based on kglr's comment will do the job.

formatQuantity[value_, name_, unit_, formatter_] :=
  formatter[N[UnitConvert[Quantity[value, name], unit]]]

Then you can do

formatQuantity[1, "SpeedOfLight", "meters/sec", ScientificForm]

c

and

formatQuantity[1, "AstronomicalUnit", "meters", EngineeringForm]

au

$\endgroup$
  • $\begingroup$ I din't realize passing arguments as machine precision will make such a big difference. Should I start cultivating the habit of mentioning everything (except for iteration counters, and others that are meant to be int dtype) as float ? $\endgroup$ – John Honai Dec 29 '17 at 13:53
  • 1
    $\begingroup$ @JohnHonai. No, in general that would be a bad habit. For numeric calculation one should consider whether exact numbers, arbitrary precision numbers or machine numbers should be used on case-by-case basis. In some cases, like this one, machine arithmetic is best, but only because forcing basic physical quantities to be expressed as machine numbers (they are stored as exact quantities, integers or rationals) is needed to get around what some might considered a quirk in front-end's display logic. $\endgroup$ – m_goldberg Dec 29 '17 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.