Parsing 10V + 100mV in Octave?

Hi all,

I don´t know if it is the appropriate place to put this question, so sorry in advance.

I would like to know if there is a package or smth so I could use different units in Octave like:

10V + 100mV
ans = 10V1

I think it would be great to have this kind of intelligence in the parser.
I also would appreciate if anyone could indicate a software with this type of functionality.

Many thanks
Itamar

To my knowledge, no such tool exists for Octave.

Regarding understanding of natural language, maybe Wolfram Alpha does not a bad job for you:

This seems to be a common obsession with electrical engineers (I used to be one so I know).

Adding the capability to the input parser is not particularly easy, and it would have to be quite customizable because presumably one would also like to write

10A + 100mA
ans = 10A1

and have this refer to Amperes. And this could be repeated for other units like ‘C’ for capacitance, or would you use ‘F’ for Farads? And what happens if someone mixes units such as

10A + 100mV

Should that throw an error?

I find that a pretty reasonable solution is to use engineering notation. Try help format to see all of the details or format short eng to just try it out. This uses scientific notation in powers of 3 so it is quite easy to see whether a value is measure in K(ilo) versus G(iga), etc., etc.

octave:1> format short eng
octave:2> 100e3 + 2e3
ans = 102.0000e+003

You might also look at the function output_precision. For example,

octave:9> output_precision (1)
octave:10> ans
ans = 102e+003

If you really want to display the output in a specific manner you can always write your own function that takes a number and returns a string of exactly the form you want.

F should clearly be for Fahrenheit and C Celsius. :roll_eyes:

Apart from symbols having multiple possible meanings, there is no way that we can change the parser to do this and remain compatible with Matlab.

If you want your numerical values to have units associated with them and for calculations to throw errors if the units don’t agree, then you’ll need to do a lot of work to define a number object that makes that possible. I think it’s out of the scope of Octave.

Here’s a really trivial (not performance-oriented) solution for display. The units input parameter is a string so you can have it display ‘V’, ‘A’, “Ohm”, or whatever you like.

function str = disp_units (val, unit = "")

  ## Save existing format and precision
  [fmt, spc, uc] = format ();
  prec = output_precision ();
  unwind_protect
    format short eng lowercase
    output_precision (2);
    str = disp (val);
    str = strrep (str, "e+000", [unit]);
    str = strrep (str, "e+003", ["K" unit]);
    str = strrep (str, "e+006", ["M" unit]);
    str = strrep (str, "e+009", ["G" unit]);
    str = strrep (str, "e-003", ["m" unit]);
    str = strrep (str, "e-006", ["u" unit]);
    str = strrep (str, "e-009", ["n" unit]);
  unwind_protect_cleanup
    ## Restore format and precision
    format (fmt, spc, uc);
    output_precision (prec);
  end_unwind_protect

endfunction

disp_units.m (1.5 KB)

Also attached as an m-file.

In action,

octave:1> disp_units (3.1e3, 'V')
ans = 3.1KV

octave:2> disp_units (9.5e-9, "sec")
ans = 9.5nsec

I think it is doable in a parser.
Different units? Yes, throw an error.
Common letters for different units? It should be a workspace or namespace directive, like namespace(‘electrical’) with the units definitions, so F is Faraday in this namespace and Fahrenheit in others.
Good discussion here !
I will take a look in Wolfram. Thanks @siko1056 !