Skip to content

Add floor divisions operator#6891

Merged
bcardiff merged 2 commits intocrystal-lang:masterfrom
bcardiff:feature/floor-div
Oct 11, 2018
Merged

Add floor divisions operator#6891
bcardiff merged 2 commits intocrystal-lang:masterfrom
bcardiff:feature/floor-div

Conversation

@bcardiff
Copy link
Member

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
@ghost
Copy link

ghost 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
Copy link
Member Author

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
Copy link
Member Author

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

Copy link
Member

@asterite asterite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@bcardiff
Copy link
Member Author

@asterite done

@bcardiff bcardiff merged commit 62fe868 into crystal-lang:master Oct 11, 2018
@RX14
Copy link
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?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants