1

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?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
hedgepig
  • 113
  • 5
  • NumberForm or one of the related numeric formatting wrappers might have the capabilities you need. – Daniel Lichtblau Aug 27 '15 at 21:57
  • @DanielLichtblau, from my understanding NumberForm allows me to specify the significant figures of a number but not read them... is this correct? – hedgepig Aug 27 '15 at 21:59
  • If those are your INPUTS,just input them as strings ... – Dr. belisarius Aug 27 '15 at 22:09
  • @belisarius, I am not entirely familiar with Mathematica - ToString[] still converts my numbers and takes out trailing zeroes it seems... – hedgepig Aug 27 '15 at 22:22
  • No wait. I'm going the other way around. What method are you using to input your values? – Dr. belisarius Aug 27 '15 at 22:24
  • Possible duplicate: (83698) – Mr.Wizard Aug 27 '15 at 22:30
  • @belisarius, I'm basically inputting them verbatim as my lab document says to. – hedgepig Aug 27 '15 at 23:19
  • You can, I think, manipulate the NumberForm by invoking ToString. Might take some fiddling around though. – Daniel Lichtblau Aug 27 '15 at 23:22
  • I should mention that all this is probably the wrong way to go about it. Check Precision and Accuracy and RealDigits; those should have what you need in order to get significant figures. – Daniel Lichtblau Aug 27 '15 at 23:24
  • @DanielLichtblau, I'm not interested in the precision of Mathematica numbers, I am merely interested in counting the number of significant figures in a number according to the rules for rounding to the number of least significant figures. RealDigits does not seem to actually tell me how many digits are in the original input... – hedgepig Aug 27 '15 at 23:36
  • @Mr.Wizard, I've attempted the first example in your solution but I am getting the output echo[$13] for input echo[1*^99, 2^2] on a new notebook (with other notebooks loaded). Frankly I'm not a regular Mathematica user, and had no idea how deep this rabbit hole was. Am I missing a command? – hedgepig Aug 27 '15 at 23:42
  • 1
    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"? – IPoiler Aug 28 '15 at 00:31
  • @inkyvoyd I am semi-inactive on Stack Exchange at the moment, just doing moderator duties, but I'll try to look at that example tomorrow. What (in my opinion) you are missing is that Mathematica directly interprets the input 00001.343200 as 1.3432` -- these are literally identical as far as the system is concerned. So if you need to preserve exact input forms it is better to use the String expression type, i.e. "00001.343200". If you describe what you wish to accomplish people can probably help you find a solution, even if it's not exactly what you had in mind. – Mr.Wizard Aug 28 '15 at 00:32
  • @It'sPronouncedOiler, I was wondering if I could convert an input into a string without having to type quotes... if that is not possible, an answer briefly explaining why would suffice. Thank you. – hedgepig Aug 28 '15 at 01:21
  • 1
    @inkyvoyd How would you distinguish this input from normal numbers? Adding two characters " is pretty concise already is it not? – Mr.Wizard Aug 28 '15 at 03:17
  • I believe you can use results from Precision and Accuracy to assess number of significant figures. – Daniel Lichtblau Aug 28 '15 at 14:08
  • 3
    I'm voting to close this question as off-topic because the OP is asking for the impossible – m_goldberg Aug 28 '15 at 15:06
  • you could mess around with $PreRead. It would be a lot of headache just to avoid typing quote marks around the numbers when you key them in though. (I actually think this is a dup, but I cant find it at the moment) – george2079 Aug 28 '15 at 17:07
  • 2
    http://mathematica.stackexchange.com/a/71577/2079 – george2079 Aug 28 '15 at 17:15
  • @Mr.Wizard, It's my fault for improperly specifying in the original question as to why I would not like to add quotes to my inputs. I am trying to write a function (for a school assignment) that calculates percent error from very specific input that I'm expected to copy and paste. Ideally, a TA could copy and paste a list of numbers into Mathematica without having to type quotes, and come up with the expected answer. I understand that my request may be impossible (in which case, I would not object to a close question), or that I might be attempting to be "difficult" (I apologize for that.) – hedgepig Aug 28 '15 at 18:21

3 Answers3

3

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"
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
2

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

Kyle Keane
  • 212
  • 1
  • 5
2

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

Greg Hurst
  • 35,921
  • 1
  • 90
  • 136