Le lundi 09 février 2015 à 08:03 -0800, Seth a écrit :
> Inspired by a Lint.jl message:
>
>
>
> INFO In 0.4+, replace int() with Int()
>
>
>
>
> I wanted to find out whether Int() was actually functionally the same
> as int(). Short answer: it's not, but I'm not sure whether (if int()
> is really out of favor in 0.4 as the Lint.jl message suggests) these
> constructors have been considered:
>
>
> julia> Int(444.55) # this works with int()
> ERROR: InexactError()
> in call at base.jl:36
>
>
> julia> Int("444") # ditto
> ERROR: MethodError: `convert` has no method matching
> convert(::Type{Int64}, ::ASCIIString)
>
>
>
>
>
> As an aside: since int(444.55) works, and int("444") works, what was
> the decision behind not allowing int("444.55")?
>
>
> julia> int("444.55")
> ERROR: ArgumentError: invalid base 10 digit '.' in "444.55"
>
>
> ... or am I missing some fundamental concept here?
The idea is that Int() is both a constructor and a call to convert(),
and it will never coerce the data to fit into an Int. For example "444"
can be converted without any loss to and Int, while 444.55 cannot. You
now need to specify how you want the coercion to occur by calling
round(), floor(), etc., since there is no "natural" way transforming a
float to an integer.
For more details you can see
https://github.com/JuliaLang/julia/issues/1470
Regards