convert error: 'Int' to 'IntLiteral' trying to implement the FloatLiteral.__round__(ndigits) method

I'm trying to contribute implementing the FloatLiteral.round() method with the current implementation:
@always_inline("nodebug")
fn __round__(self, ndigits: IntLiteral = 0) -> Self:
"""Return the rounded value of the FloatLiteral.

Args:
ndigits: The number of digits to round to. Defaults to 0.
Returns:
The rounded value.
"""
# Handle special values first.
if not self._is_normal():
return self

var pow: IntLiteral = 10 ** ndigits
var to_round: Self = self * Self(pow)
var truncated: IntLiteral = to_round.__int_literal__()
var result: Self
if self.__abs__() - Self(truncated).__abs__() < 0.5:
result = Self(truncated)
elif self > 0:
result = Self(truncated + 1)
else:
result = Self(truncated - 1)
if ndigits > 0:
result = result / (Self(10.0) ** ndigits)
return result
@always_inline("nodebug")
fn __round__(self, ndigits: IntLiteral = 0) -> Self:
"""Return the rounded value of the FloatLiteral.

Args:
ndigits: The number of digits to round to. Defaults to 0.
Returns:
The rounded value.
"""
# Handle special values first.
if not self._is_normal():
return self

var pow: IntLiteral = 10 ** ndigits
var to_round: Self = self * Self(pow)
var truncated: IntLiteral = to_round.__int_literal__()
var result: Self
if self.__abs__() - Self(truncated).__abs__() < 0.5:
result = Self(truncated)
elif self > 0:
result = Self(truncated + 1)
else:
result = Self(truncated - 1)
if ndigits > 0:
result = result / (Self(10.0) ** ndigits)
return result
But I'm receiving the error: cannot implicitly convert 'Int' value to 'IntLiteral' in 'var' initializer in the var pow: IntLiteral = 10 ** ndigits line If I change the type to from IntLiteral to Int this will move the error to the following line, the to_round declaration .
1 Reply
msaelices
msaelicesOP7mo ago
Now I'm not sure if it's a compiler issue, as I also found an error with this minimalistic example:
fn get_intliteral() -> IntLiteral:
return IntLiteral(10 ** 2)
fn get_intliteral() -> IntLiteral:
return IntLiteral(10 ** 2)
I'm getting error: no matching function in initialization and in the error details candidate not viable: argument #0 cannot be converted from 'Int' to '!kgen.int_literal'
Want results from more Discord servers?
Add your server