Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time::Span#*, Time::Span#/ and Float #5272

Closed
nome opened this issue Nov 10, 2017 · 4 comments
Closed

Time::Span#*, Time::Span#/ and Float #5272

nome opened this issue Nov 10, 2017 · 4 comments

Comments

@nome
Copy link

nome commented Nov 10, 2017

Trying to calculate an estimate of remaining time for a task based on elapsed time and percentage done, using the following code:

done = 0.75 # 75% of work done
elapsed = Time::Span.new(0,1,0)
total = elapsed / done

I see the following error:

Error in line 3: instantiating 'Time::Span#/(Float64)'

in /scr/hallasan/crystal-0.24.0-2/src/time/span.cr:303: no overload matches 'Int64#tdiv' with type Float64
Overloads are:
 - Int#tdiv(other : Int)

    seconds = to_i.tdiv(number)
                   ^~~~

Alternatively:

done = 0.75 # 75% of work done
elapsed = Time::Span.new(0,1,0)
total = elapsed * (1.0/done)

results in

Error in line 3: instantiating 'Time::Span#*(Float64)'

in /scr/hallasan/crystal-0.24.0-2/src/time/span.cr:296: no argument named 'seconds'
Matches are:
 - Time::Span.new(days : Int, hours : Int, minutes : Int, seconds : Int, nanoseconds : Int = 0)
 - Time::Span.new(hours : Int, minutes : Int, seconds : Int)
 - Time::Span.new(*, seconds : Int, nanoseconds : Int)
 - Time::Span.new(*, nanoseconds : Int) (trying this one)

    Span.new(
         ^~~

So, neither Time::Span#/ nor Time::Span#* appear to accept floats. However, the API documentation gives their types as #*(number : Number) and #/(number : Number), and 0.75.is_a? Number # => true.

Am I missing something or is this a bug?

$ crystal -v
Crystal 0.23.0+313 [ea4187c57] (2017-10-27)

LLVM: 3.9.1
Default target: x86_64-unknown-linux-gnu
@petoem
Copy link
Contributor

petoem commented Nov 10, 2017

@nome you are using crystal compiler version 0.23.0+313 with crystal-0.24.0-2 stdlib.
Anyway I don't think this is an issue here.

Both Time::Span#/ and Time::Span#* are bugged.
Time::Span#/ accepts a type Number but tries to use Int#tdiv which only accepts Int not Number and certainly not Float64.

In Time::Span#* on line https://github.com/crystal-lang/crystal/blob/master/src/time/span.cr#L297
it does Int64 * Float64 which results in Float64, hence Time::Span.new fails because there is no named argument called seconds with type Float64.

@petoem
Copy link
Contributor

petoem commented Nov 10, 2017

@nome As a workaround you can use the total_seconds of the elapsed timespan to calculate and then create a new Time::Span from the total with #seconds method.

done = 0.75 # 75% of work done
elapsed = Time::Span.new(0,1,0)
total = elapsed.total_seconds / done
pp total.seconds # => 00:01:20

@akzhan
Copy link
Contributor

akzhan commented Nov 10, 2017

/cc @asterite

@nome
Copy link
Author

nome commented Nov 11, 2017

@petoem Thanks, the workaround does help.

(incidentally, I've been using compiler and stdlib from the 0.24.0 pre-release tarball, so I disavow responsibility for any version mismatches ;-))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants