Hello!
May be answer to my question is already in the documentation, but I still ask: why a can't mix types in div operation:
var a,b: int
var c: int64
echo a + b + c, a + b * c
compile without any error, but when i try
var a,b: int
var c: int64
echo a + b + c, a + b / c
I have Error: type mismatch: got (int, int64) but expected one of: system./(x: float, y: float) system./(x: int, y: int) system./(x: float32, y: float32)
Is there some sort of generic '/' operator?
Result of b / c is float, so you add int and float. When result would be automatically promoted to float and int is 64 bit, the result may become inaccurate.
I guess you want b div c for integer division. Or you may want type conversion like float(i) or int(x). I think float(i) may raise an exception when i is 64 bit on your system and it is too large to fit into float. You may test yourself.
Hi. First, you do realize that your variables are not initialized?
Second, Nim has very strict type behavior. Nim also has two division operator. The / operator means float division while *div* is integer division.
I assume you meant integer division.
echo(a + b + c, " ", a + b div c)
if not then you need to explicitly convert c to int : int(c) or c.int() or c.int, it means the same thing. a must also be explicitly converted to float because (int) / (int) -> float and the + operator is not defined for (int, float).
echo(a + b + c, " ", a.float + b / c.int)
Hi, thanks for answers! Yes, i know about not initialized vars, it is just example, not real code.
For me it is a bit strange that I can't (this code not compile):
var a: int
var b: int64
echo a / b
but can (this code compiles without error)
var a: int
var b: int64
echo a * b
I think it is all about implicit type conversions, but not clearly understand why. It is actually easy once you understand the logic and indeed it depends on implicit type conversions .
see implict type conversion Usually smaller integer (and float-types) type will implicitly convert to wider types.
Identical integer (and float-types) will have the various arithmetic operator defined. (except div for float-like type, obviously)
You can check for defined operators for types in: system.nim
for your problem you can defined a new proc for int64: (int implicitly converting to int64)
proc `/`(x, y: int64): float = x.float / y.float
now it works (obviously you loose some precision for larger int64 numbers since float is an alias for c double)
var a: int
var b: int64
echo a / b
What is referred to here as integer division is also known as floor division.
The Python language offers normal division that returns a floating point number through the / operator and integer/floor division through the // operator.
I find this much more elegant, as it both leaves the div keyword to be used for a different purpose that is more appropriate and necessary (think about how Karax goes about its business of defining divs), and it looks more mathematical by using a symbol instead of a keyword. It is also more concise being just two characters.
So learn some Nim before complaining:
template `//`(a, b: untyped) : untyped = a div b
I couldn't copy Python's design because back then Python only had / and it sucked so I had to copy a different design.
as it both leaves the div keyword to be used for a different purpose that is more appropriate and necessary
Maybe for you, but I think division is a necessary operation.
Besides div is not a keyword, you can overload it:
proc `div`(body: string): string =
"<div>\n" &
" " & body & "\n" &
"</div>"