开发者

What division operator symbol would you pick?

I am currently designing and implementing a small programming language as an extra-credit project in a class I'm taking. My problem is that the language has three numeric types开发者_StackOverflow: Long, Double, and Fraction. Fractions can be written in the language as proper or improper fractions (e.g. "2 1/3" or "1/2"). This fact leads to problems such as "2/3.5" (Long/Double) and "2/3"(Long/Long) not being handled correctly by the lexer.The best solution that I see is to change the division operator. So far, I think "\" is the best solution since "//" starts comments.

Would you pick "\", if you were designing the language?

Would you pick something else? If so, what?

Note: changing the way fractions are written is not possible.

Thanks in advance for your help,

-EDIT-

Current operators are:

** (power)

* (multiply)

% (modulus)

+ (add)

- (subtract and negation)

~ (reciprocal)

! (NOT)

> (greater)

< (less)

>= (greater-equal)

<= (less-equal)

== (equality)

<> (inequality)

& (AND)

| (OR)

^ (XOR)

?? (null-coalesing)

= (assignment)

Note: I am under no strictly binding demands by my teacher; however, I basically convinced him that I could turn a simple fraction calculator into a language. Changing the fraction's syntax would go against the fraction calculator's syntax for fractions, which is preferably avoided.


The APL answer is ÷. I think that's recognizable.


How about a word "div"?


Are you allowing compact if statements like

condition ? trueblocks : falseblocks;

in your grammar? If not, you could use ':' as your division operator. It is after all the standard mathematical symbol for division (by hand, on paper) in many (most?) places around the word.


I'm not quite sure what's the problem, since I don't know grammar theory. But I would try changing proper fraction syntax, e.g. to 2_1/3, with underscore rather than space.

About using \ as division operator — I would certainly not like it, since it is used in about every language as an escape character. I wouldn't recommend such overload of meanings.


Another reason to avoid using \ as a replacement for / is because it may cause confusion as to which operand is the numerator and which is the denominator. For example, MATLAB arithmetic operators include both right division / and left division \. The value of 1/2 is 0.5, but the value of 1\2 is 2. In other words, / has the numerator on the left, and \ has the numerator on the right.


If it's for a class, I would simply use a keyword like dividedby. Otherwise you are going to have to figure out what the / means by its context. There really aren't any other single characters that are suitable.

Be aware, however, that your teacher may be asking you to solve the contextual problem, in which case he wants you to use the slash. I would ask.


What about requiring Fraction type to use a double in all cases; ie, 2.0 / 3.5 vs 2 / 3.5 Then no new operator needed.


I'd avoid \ like crazy since it would confound people's expectations.

I would go ahead and use / but get the problem out of the lexer and into the parser where it will be easier to deal with and you can issue sensible error messages.


I think the two-character /| is a possible idea. / is the division and | looks like fraction rotated 90 degrees.

If you allow bracket or unary operators, I could recommend you [2 1/2] or #1/2.


How about making fractions the primary data type? Thinking of the division operator as an infix constructor?


You could adopt J's somewhat nonstandard convention of % for division (because it kinda looks like the division symbol) and | for modulo.

But then you have to find something else for bitwise-or. Perhaps /?


I will not pick '\' because it is less accesible than '/' in keyboards. and also, it is more widely used. so your programmers will fell more in home.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜