1
$\begingroup$

I'm experimenting with units:

UnitConvert[1/Quantity[2.0, "GHz"]]

5.*10^-10s

fs = Quantity[2,"GHz"]
UnitConvert[1/fs]

1/2000000000s

Two questions:

  1. How to ask Mathematica to automatically display the result using the proper prefix ($ms$, $\mu s$, $ns$, ...) instead of systematically using the base SI unit ($s$)
  2. Why is the result displayed as a fraction in the second example, whereas it wasn't in the first example? I know I can force numerical evaluation using N[%], but I don't understand why I need to do it.
$\endgroup$
  • $\begingroup$ For the first, if you want microseconds, just ask for those as in UnitConvert[1/fs, "microseconds"]. This might not satisfy your desire for being automatic. In the second example, you used an integer and by default, Mathematica keeps fractions when the dividend and divisor are integers. $\endgroup$ – Mark R Dec 8 '19 at 2:59
  • $\begingroup$ I think the first part of your question has already been answered here mathematica.stackexchange.com/questions/57867/… For the second part, just make sure to always use 2. or 2.0 instead of 2. This forces Mathematica to interpret the number as machine precision. Mathematica has powerful features for dealing with exact numbers and assumes numbers without decimals are exact. For example, 1/3 + 1/3 yields 2/3. Most computer languages would give an incorrect answer of 0.6666666666666666. $\endgroup$ – MassDefect Dec 8 '19 at 3:00
  • $\begingroup$ Mark, MassDefect, thanks to both of you for having pointed me to the right direction. FWIW, I didn't even notice in one case I wrote 2.0 whereas in the second case I wrote 2 :D But that was a good occasion to learn more about the subtleties of Mathematica! $\endgroup$ – Sylvain Leroux Dec 8 '19 at 3:05
4
$\begingroup$

As was said int the comments, you need to be more explicit to get the forms you want. That means giving a 2nd argument to UnitConvert. For example:

UnitConvert[1/Quantity[2.0, "GHz"], "Nanoseconds"]

0.5ns

and

UnitConvert[1/Quantity[2, "GHz"], "Nanoseconds"]

1/2ns

but

UnitConvert[1/Quantity[2.0, "GHz"], "Picoseconds"]

500.ps

and

UnitConvert[1/Quantity[2, "GHz"], "Picoseconds"]

500ps

When no 2nd argument appears, UnitConvert defaults to IS units. This all well documented.

Also, keep in mind the mantra: all quantities in exact, then exact quantities out; any inexact quantities in, then inexact quantities out.

|improve this answer|||||
$\endgroup$

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