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

Add floor divisions operator #6891

Merged
merged 2 commits into from Oct 11, 2018

Conversation

Projects
None yet
5 participants
@bcardiff
Member

bcardiff commented Sep 28, 2018

Adds semantics to the // operator as integer division / floor division.

a // b will represent the mathematical value ⌊a ÷ b⌋ and be of the same type as a. This is consistent with other arithmetic operators (lhs operand type is preserved).

Follow up of #6470 as part of #2968.

After next release / will be able to be changed to float division for all types and use // where floor or integer division is needed.

@bcardiff bcardiff added this to the 0.27.0 milestone Sep 28, 2018

@r00ster91

This comment has been minimized.

Contributor

r00ster91 commented Oct 5, 2018

Trying to format

struct Int
  def //
  end
end

gives a bug:

Error: couldn't format './cr.cr', please report a bug including the contents of it: https://github.com/crystal-lang/crystal/issues

expecting keyword end, not `/, `, at :4:8 (Exception)
  from ???
  from ???
  from ???
  from ???
  from ???
  from ???
  from ???
  from ???
  from ???
  from ???
  from ???
  from ???
  from ???
  from ???
  from ???
  from ???


I think // needs to be added in this tuple: formatter.cr:2171 or maybe here: formatter.cr:2188.

@bcardiff

This comment has been minimized.

Member

bcardiff commented Oct 6, 2018

Yes this is still WIP or blocked do to the formatted. I will check if that is enough. I’m not that familiar with the formatter still. Since parser and formatter use the lexer with its own control fow the state of the lexer evolves differently for the same input and is hard to keep them aligned i think.

@bcardiff bcardiff force-pushed the bcardiff:feature/floor-div branch from 5d48f5e to 7eedaf0 Oct 11, 2018

@bcardiff

This comment has been minimized.

Member

bcardiff commented Oct 11, 2018

Rebased on to master. Ready for another review (while waiting for CI)

@asterite

I think some tests are missing for floats that aren't exactly integers. For example 7.5 // 2.1 and such.

@bcardiff

This comment has been minimized.

Member

bcardiff commented Oct 11, 2018

@asterite done

@bcardiff bcardiff merged commit 62fe868 into crystal-lang:master Oct 11, 2018

4 checks passed

ci/circleci: test_darwin Your tests passed on CircleCI!
Details
ci/circleci: test_linux Your tests passed on CircleCI!
Details
ci/circleci: test_linux32 Your tests passed on CircleCI!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@RX14

This comment has been minimized.

Member

RX14 commented Oct 13, 2018

Can we add a -D flag in 0.27.0 which will make / return float, to allow people to test their code with the new behaviour before 0.28.0?

omarroth added a commit to omarroth/crystal that referenced this pull request Nov 5, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment