1
$\begingroup$

I'm trying to write a function that counts the number of significant figures a given number has so I can ensure my calculations are rounded correctly; however, I am having a lot of trouble preventing the formatting engine from changing my inputs. For instance,Defer[00001.343200] returns 1.3432 when I would like a way for the output to keep the raw input of a number as the original 00001.343200 or, failing that, 1.343200. ToString does the same thing. Phrased differently, I would like to suppress whatever is converting my input with leading and or trailing zeroes to an output that lacks leading and trailing zeroes.

Does anyone know how to do this, or find an equivalent solution?

$\endgroup$
  • $\begingroup$ NumberForm or one of the related numeric formatting wrappers might have the capabilities you need. $\endgroup$ – Daniel Lichtblau Aug 27 '15 at 21:57
  • 1
    $\begingroup$ Just to be clear, if you just want to maintain inputs in the form you specify then you've tried entering the numbers as a string such as "00001.343200"? $\endgroup$ – IPoiler Aug 28 '15 at 0:31
  • 1
    $\begingroup$ @inkyvoyd How would you distinguish this input from normal numbers? Adding two characters " is pretty concise already is it not? $\endgroup$ – Mr.Wizard Aug 28 '15 at 3:17
  • 3
    $\begingroup$ I'm voting to close this question as off-topic because the OP is asking for the impossible $\endgroup$ – m_goldberg Aug 28 '15 at 15:06
  • 2
    $\begingroup$ mathematica.stackexchange.com/a/71577/2079 $\endgroup$ – george2079 Aug 28 '15 at 17:15
3
$\begingroup$

I think what you want can be probably be done with $PreRead, similarly to How to print unevaluated arguments in the form they were originally typed in? which I proposed as a possible duplicate. (If you need modified behavior for one particular Cell but not others see also CellEvaluationFunction). In that answer I gave code that fairly robustly lets a function act as though it holds raw input.

Better I think is to create a Palette that processes your data as necessary at the click of a button; see Szabolcs's Paste Tabular Data palette which I mentioned here for an example. If I knew what you were going to do with the input I might try to modify that code to produce a working example.

Lastly a possibly simpler approach that might work is to automatically convert numbers into Strings on input, then write your function(s) do to whatever you want from there. A first attempt:

$PreRead = # /. s_String /; StringMatchQ[s, NumberString] :> "\"" <> s <> "\"" &;

Now entering e.g.:

000123.45600 + 007

Is converted automatically, before evaluation, to:

"000123.45600" + "007"
$\endgroup$
2
$\begingroup$

Aside from the fun evaluation control and numerical precision discussions happening, is this useful to accomplish the task you wanted? It does simply use string patterns to drop the appropriate zeroes.

sigfigs[str_String] : = StringLength @ 
    If[
        StringContainsQ[s, "."], 
        StringTrim[
            StringReplace[s, "." -> ""], 
            "0" .. ~~ Except[EndOfString]
        ],
        StringTrim[s, "0" ..]
    ]

this gives

  • sigfigs @ "0.0001" -> 1
  • sigfigs @ "01.00" -> 3
  • sigfigs @ "1000" -> 1

Please alert me if I've done something silly, it is late and I am prone for err in this state.

Cheers

$\endgroup$
2
$\begingroup$

Reading your comment about not wanting your TA's to type the quotes of a string, you could build some sort of user interface.

Here's a very crude one.

list = {};
DynamicModule[{f = ""}, 
  Column[{
    InputField[Dynamic[f], String], 
    Dynamic[If[f =!= "", AppendTo[list, f]; f = ""]; list]
  }]
]

enter image description here

$\endgroup$

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.