Confusion about / and //

Hello, crystal inherits wisdom from ruby,
but all of a sudden, / and // changes to follow python semantics,
which is quite confusing and why is that?

in ruby // is empty regex, you can try that.

puts //

There is some context in https://github.com/crystal-lang/crystal/pull/8120 and the related issue.

readed, but it seemed already introduce / and // and don’t discuss
the reason why in first place use / to replace fdiv?

Also another question is
Float64 < BigFloat < BigDecimal,
what’s difference between BigFloat and BigDecimal,
and why BigFloat < BigDecimal?

Make `/` be float division · Issue #2968 · crystal-lang/crystal · GitHub Sums it up well imo.

My understanding is while BigFloat can handle very large float values, it is not suitable to precise calculations with exact decimal values, which is where BigDecimal comes into play.

BigFloat doesn’t inherit from BigDecimal?

oh, sorry, I mean the link you give me
the first comment,

Andmaxis computed under the partial order inferred by{Float32 < Float64, Float64 < BigFloat < BigDecimal < BigRational, Float64 < Complex}

Oh, in that case I don’t think I can really speak to that. Would have to ask Brian to elaborate.

The < relation mentioned in

And max is computed under the partial order inferred by {Float32 < Float64, Float64 < BigFloat < BigDecimal < BigRational, Float64 < Complex}

is not the inheritance. Note that Float32 does not inherit from Float64 either.

Probably there was a less cryptic way to explain all the rules.
Or be more strict and restrict some operations to operands of the same type.

Am I missing anything else that should be clarified regarding / and //?

yeah, I know you don’t mean inheritance there,
but why BigFloat is partial order < BigDecimal ?
(and don’t worry about symbol here, partial order does use < symbol)
weak partial order is a<=b, strong partial order is a<b)

1.does BigFloat falls within the range of BigDecimal
2. does every number representable by BigFloat also representable by BigDecimal?

BigFloat is an arbitrary-precision integer divided by a power of 2. BigDecimal is an arbitrary-precision integer divided by a power of 10. Therefore every BigFloat is exactly representable by a BigDecimal, although the latter is far less efficient for the same fractional digits. (GMP floats are a strict subset of decimals because they cannot represent infinities and not-a-numbers, whereas MPFR floats aren’t a strict subset.)

3 Likes