Hi all, I need to do some arithmetic ops like this.
var loopEnd : int = 300
for i in 0 ..< loopEnd
var r,g,b : uint
r = cast[uint](c1.red + (i * (c2.red - c1.red) / loopEnd ))
But I am facing type mismatch errors. Which is the correct data type for these kind of ops. Please help.It seems like you don't need to use cast for this, just regular type conversion would be fine.
Nim will perform runtime checks on type conversions to make sure that you don't get overflow when converting between integer types, except for signed -> unsigned conversions at runtime.
To resolve the errors, all the arithmetic operations need to be between values of the same type, and the result of the expression must match the variable you're assigning to.
For example, you could solve it like this:
var loopEnd = 300
for i in 0 ..< loopEnd
var r, g, b: uint
r = c1.red + (i.uint * (c2.red - c1.red) / loopEnd.uint)
Or like this:
var loopEnd = 300
for i in 0 ..< loopEnd
var r, g, b: uint
r = (c1.red.int + (i * (c2.red.int - c1.red.int) / loopEnd)).uint
Or like this:
var loopEnd = 300'u
for i in 0'u ..< loopEnd
var r, g, b: uint
r = c1.red + (i * (c2.red - c1.red) / loopEnd)
@exelotl, Thanks for the reply. Problem solved. This is the code.
r = (c1.red.int + ((i * (c2.red - c1.red).int / loopEnd)).int).uint
g = (c1.green.int + ((i * (c2.green - c1.green).int / loopEnd)).int).uint
b = (c1.blue.int + ((i * (c2.blue - c1.blue).int / loopEnd)).int).uint