coffeescript减法中的奇怪行为

时间:2014-06-30 12:03:46

标签: coffeescript

我一定是疯了。对于两个不等的整数之间的差异,Coffeescript继续返回'0'。

x = @location[0]
y = @start[0]
console.log "#{x} - #{y} = #{x-y}"

这是输出:

350 - 322 = 0
250 - 278 = 0
... and so on

我完全糊涂了!

编辑:这是咖啡和编译的js

中的来源
class Branch
    constructor: (@length, @start, @angle, @generation, @parent) ->
        return if @generation >= RECURSION_LIMIT
        @angleVariation = pi/3
        @ratio = 2/(1+root(5))
        @maxChildren = 10
        @parent ?= null
        @unit = 2
        @children = []
        @location = @start
        @childLocations = [Math.random()*@length*@ratio/2+(@length*@ratio) \
            for n in [0...@maxChildren]]

    grow: ->
        gl.beginPath()
        moveTo @location
        @location[0] += @unit * cos(@angle)
        @location[1] -= @unit * sin(@angle)
        lineTo @location
        gl.stroke()
        console.log @getLength()
        if @getLength() >= @childLocations[@children.length]
            @birthChild()


    getLength: ->
        x = @location[1]
        y = @start[1]
        console.log "#{x} - #{y} = #{x-y}"
        return 1 #root( (@location[0]-@start[0])**2 + (@location[1]-@start[1])**2 )

    birthChild: ->
        angle = @angle + (Math.random()*@angleVariation*2) - @angleVariation
        child = new Branch @length * @ratio, @location, angle, @generation+1, this

在js:

Branch = (function() {

  function Branch(length, start, angle, generation, parent) {
    var n, _ref;
    this.length = length;
    this.start = start;
    this.angle = angle;
    this.generation = generation;
    this.parent = parent;
    if (this.generation >= RECURSION_LIMIT) {
      return;
    }
    this.angleVariation = pi / 3;
    this.ratio = 2 / (1 + root(5));
    this.maxChildren = 10;
    if ((_ref = this.parent) == null) {
      this.parent = null;
    }
    this.unit = 2;
    this.children = [];
    this.location = this.start;
    this.childLocations = [
      (function() {
        var _i, _ref1, _results;
        _results = [];
        for (n = _i = 0, _ref1 = this.maxChildren; 0 <= _ref1 ? _i < _ref1 : _i > _ref1; n = 0 <= _ref1 ? ++_i : --_i) {
          _results.push(Math.random() * this.length * this.ratio / 2 + (this.length * this.ratio));
        }
        return _results;
      }).call(this)
    ];
  }

  Branch.prototype.grow = function() {
    gl.beginPath();
    moveTo(this.location);
    this.location[0] += this.unit * cos(this.angle);
    this.location[1] -= this.unit * sin(this.angle);
    lineTo(this.location);
    gl.stroke();
    console.log(this.getLength());
    if (this.getLength() >= this.childLocations[this.children.length]) {
      return this.birthChild();
    }
  };

  Branch.prototype.getLength = function() {
    var x, y;
    x = this.location[1];
    y = this.start[1];
    console.log("" + x + " - " + y + " = " + (x - y));
    return 1;
  };

  Branch.prototype.birthChild = function() {
    var angle, child;
    angle = this.angle + (Math.random() * this.angleVariation * 2) - this.angleVariation;
    return child = new Branch(this.length * this.ratio, this.location, angle, this.generation + 1, this);
  };

  return Branch;

})();

0 个答案:

没有答案