Befunge - 37 x 5 = 185 38 x 3 = 114 characters
This is limited to integer numbers as Befunge has no floating point support.
&v /& _ #`&# "-"$# -#< v
>~:0`!#v_:" "`! #v_:","`#^_"*"`#v_&*>
^ ># $ .# @#< >&+
Explanation
The biggest distinguishing feature of Befunge is that instead of being a linear set of instructions like most languages; it is a 2d grid of single character instructions, where control can flow in any direction.
The first &
simply inputs the first number. The v
and >
then redirect control to the main path on the second row.
~:0`!#v_
This inputs a character (~
), duplicates it (:
), pushes zero onto the stack (0
), pops the top two elements and determines if the second is greater than the first (`
I'm surprised you can't use ``` to get code backticks.), inverts the truthiness of the top element (!
), then goes right if it is zero, down otherwise (#v_
).
Basically it's checking whether the input is -1
representing no more input.
># $ .# @
If the input was -1
then the duplicated input value is discarded ($
), the top of the stack is output as an integer (.
) and the program is halted (@
).
:" "`! #v_
Otherwise a similar process is repeated to determine if the input is less than or equal to a space. If it is a space then control goes down, otherwise control heads right.
^ ># $ .# @#<
If it is a space then it's redirected left (<
); the program halt (@
), output (.
) and right redirection (>
) are all skipped using #
; but the discard is executed to remove the space from the stack. Finally it's redirected up to begin the next execution (^
).
:","`#^_
If it wasn't a space the same process is used to split on if it is in [+, *]
or in [-, \]
going right and up respectively.
>~ "*"`#v_&*>
^ >&+
For [+, *]
it is again split to determine whether it is a +
or a *
. If +
it is directed down then the next number is input (&
) and they are added (+
), the control then wraps around and is redirected up to the main path for the next character. If *
then it inputs (&
) and multiplies (*
) then directly wraps around.
/& _ #`&# "-"$# -#<
For [-, \]
it starts on the right heading left. The #
's skip the character after them so the initial path is "-"`_
which simply determines if it is -
or /
. If it is /
then it continues left to input (&
) and divide (/
). If it is -
then it heads right, again skipping characters so that it executes &"-"$-
resulting in the number being input (&
) the -
character being pushed onto the stack then discarded ("-"$
) and then the subtraction being calculated (-
). The control is then redirected back to the main path.
1What about using command line arguments ARGV? because the shell auto-splits and lists the arguments. – Ming-Tang – 2011-02-06T03:01:05.400
This is really trivial if you're just calculating left to right, and not using standard operator precedence. I was hoping to get a chance to show off my knowledge of Dijkstra's Shunting Yard algorithm here but I guess that won't be necessary. – Rune Aamodt – 2011-02-06T15:03:56.220
How much floating-point precision does it have to support? – ASCII-only – 2017-08-08T12:32:56.820
May we use fixed-point or rational numbers instead of floating-point? – Toby Speight – 2018-01-26T10:01:45.783
An advanced calculator challenge would be fun – starbeamrainbowlabs – 2014-04-19T09:27:45.273
@starbeamrainbowlabs already exists http://codegolf.stackexchange.com/q/617/148
– Kevin Brown – 2014-04-19T22:30:32.657@KevinBrown Cool! I didn't see that. – starbeamrainbowlabs – 2014-04-20T07:45:09.280
My calculator uses postfix. See also Evaluating Mathematical Expressions on Stack Overflow for competition (though I haven't checked if the rules are identical). – dmckee --- ex-moderator kitten – 2011-02-05T19:04:23.973
3Third test case is wrong - whether you follow standard order of operations or perform all operations left to right. Looking at the second test case, does your calculator round the result of each operation? – PleaseStand – 2011-02-05T19:04:26.297
Fixed the second and third test case, the result is not rounded. – Kevin Brown – 2011-02-05T19:21:53.907
The third test case does not follow the standard order of operations. Are our answers supposed to? – John – 2011-02-05T21:35:46.973
@John, the last requirement says it needs to evaluate the string from left to right. This means it doesn't follow the standard order of operations. – Kevin Brown – 2011-02-05T21:39:45.587
@Bass: Ok, just clarifying. – John – 2011-02-05T21:41:34.740