0
$\begingroup$

I put these prefix in my init.m file. First I Unprotect all these symbols, set values and then Protect again so they cannot be changed. However, I got errors when I use ClearAll["Global'*"] as these variables are protected and not allowed to clear. I can leave these variables unprotected but this is unwanted as they can be changed probably by accident.
How would you solve this? Or do you have a better idea to do this?
The unit package doesn't help as they don't give me numerical values.

Unprotect[Yocto, Zepto, Atto, Femto, Pico, Nano, Micro, Milli, Centi, 
  Deci, Deka, Hecto, Kilo, Mega, Giga, Tera, Peta, Exa, Zetta, Yatta, 
  mySIprefix];
Remove[Yocto, Zepto, Atto, Femto, Pico, Nano, Micro, Milli, Kilo, 
  Mega, Giga, Tera, Peta, Exa, Zetta, Yatta];
mySIprefix = {"Yocto", "Zepto", "Atto", "Femto", "Pico", "Nano", 
   "Micro", "Milli", "Centi", "Deci", "Deka", "Hecto", "Kilo", "Mega",
    "Giga", "Tera", "Peta", "Exa", "Zetta", "Yatta"};
Thread[{Yocto, Zepto, Atto, Femto, Pico, Nano, Micro, Milli, Centi, 
    Deci, Deka, Hecto, Kilo, Mega, Giga, Tera, Peta, Exa, Zetta, 
    Yatta} = 
\!\(\*SuperscriptBox[\(10\), \({\(-24\), \(-21\), \(-18\), \(-15\), \(-12\), \(-9\), \(-6\), \(-3\), \(-2\), \(-1\), 1, 2, 3, 6, 9, 12, 15, 18, 21, 24}\)]\)];
Protect[Yocto, Zepto, Atto, Femto, Pico, Nano, Micro, Milli, Centi, 
  Deci, Deka, Hecto, Kilo, Mega, Giga, Tera, Peta, Exa, Zetta, Yatta, 
  mySIprefix];

Somehow I cannot reprocedure the error now.
However, I got this error when I run my init.m file.
(I have other packagage loaded before this as well) I think that the error is because of the unit package I loaded and haven't removed. The last messages are a bit weird to me though. I just removed the unit package plus my other package and the error disappeared.

enter image description here

$\endgroup$
6
  • 2
    $\begingroup$ Start the file with BeginPackage["Prefixes`"] and end the file with EndPackage[]. That way the symbols will live in their own context and won't interfere with the Global context. $\endgroup$ Commented Apr 5, 2022 at 17:16
  • $\begingroup$ ClearAll should ignore symbols that are Protected. Are you sure that's where you're getting errors? Can you provide the actual errors you're seeing. FWIW, I can't generate any errors when trying to follow your instructions. $\endgroup$
    – lericr
    Commented Apr 5, 2022 at 17:17
  • $\begingroup$ SjoerdSmit thanks @lericr somehow I cannot reprocedure it now. However, if I run it in the init.m I got this error. I'll update it in my post. $\endgroup$
    – hana
    Commented Apr 5, 2022 at 17:27
  • $\begingroup$ Ah. It looks like you had loaded the Units` context, and so you're getting a name clash because those SI prefix symbols are also in the Units` context. I would either follow Sjoerd's suggestion and create your own context, or just use the built in functionality around Quantity expressions. E.g. you can do Quantity[1, "Femto" "Meter"], and then you can do whatever arithmetic or conversions that you want. $\endgroup$
    – lericr
    Commented Apr 5, 2022 at 17:53
  • 1
    $\begingroup$ There are several other options. You could use *^ (e.g. 3*^-9 evaluates to 3*10^-9). You could define InputAliases (easiest to do through the options inspector). You could define functions instead of constants. None of these are necessarily better--just thoguht you might be interested in alternatives. $\endgroup$
    – lericr
    Commented Apr 5, 2022 at 20:36

1 Answer 1

1
$\begingroup$

With v13 there is no problem with the built-in units . What version are you using?

$Version

(* "13.0.1 for Mac OS X x86 (64-bit) (January 28, 2022)" *)

prefix = {"Yocto", "Zepto", "Atto", "Femto", "Pico", "Nano", "Micro", "Milli",
    "Centi", "Deci", "", "Deka", "Hecto", "Kilo", "Mega", "Giga", "Tera", 
   "Peta", "Exa", "Zetta", "Yatta"};

quantitiesShort = Quantity[1, # <> "meter"] & /@ prefix

enter image description here

quantitiesLong = UnitConvert@quantitiesShort

enter image description here

quantitiesLong // N // ScientificForm

enter image description here

quantities = QuantityMagnitude@quantitiesLong

enter image description here

quantities // N // ScientificForm

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks, I used V12 but I updated to V13 today. Yours work but I should have added my motivation better. I have to input something like 3*10^-9, 2*10^-12 but this is a bit long and slow to type. So I want to make some prefix like Nano, Pico so I just need to type simply as 3 Nano, 2 Pico. $\endgroup$
    – hana
    Commented Apr 5, 2022 at 21:51
  • $\begingroup$ ‘3*^-9’ and ‘2*^-12’ are just as short and don’t damage built—in for their intended use $\endgroup$
    – Bob Hanlon
    Commented Apr 5, 2022 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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