Esempio n. 1
1
  /**
   * Converts half-float to {@code float}
   *
   * @param half half-float to convert
   * @return value as {@code float}
   */
  public static float toFloat(short half) {
    int sign = (half >= 0 ? 1 : -1);
    int exponent = ((EXPONENT_MASK & half) >> 10) - 15;
    int significand = SIGNIFICAND_MASK & half;

    // infinity or NaN
    if (exponent == 16) {
      // infinity
      if (significand == 0) {
        return Math.copySign(Float.POSITIVE_INFINITY, sign);
      }
      // generic NaN, no decoding
      else {
        return Math.copySign(Float.NaN, sign);
      }
    }
    // zero or subnormal
    else if (exponent == -15) {
      // signed zero
      if (significand == 0) {
        return Math.copySign(0.0f, sign);
      }
      // subnormal
      else {
        return Math.copySign(Math.scalb(significand, exponent - 10), sign);
      }
    }
    // normal value
    else {
      // add leading bit
      significand |= SIGN_MASK;

      return Math.copySign(Math.scalb(significand, exponent - 10), sign);
    }
  }
 private Point ellipsePtFromAngle(Point center, double rh, double rv, double angle) {
   double x = center.getX();
   double y = center.getY();
   double c = Math.cos(angle);
   double s = Math.sin(angle);
   double ta = s / c;
   double tt = ta * (rh / rv);
   double d = 1.0 / Math.sqrt(1.0 + Math.pow(tt, 2));
   double ex = x + Math.copySign(rh * d, c);
   double ey = y + Math.copySign(rv * tt * d, s);
   return new Point(ex, ey);
 }
 /**
  * java level API
  *
  * @param input expects a tuple containing two numeric DataAtom value
  * @param output returns a single numeric DataAtom value, which is first floating-point argument
  *     with the sign of the second floating-point argument.
  */
 @Override
 public Float exec(Tuple input) throws IOException {
   if (input == null || input.size() < 2) return null;
   try {
     float first = (Float) input.get(0);
     float second = (Float) input.get(1);
     return Math.copySign(first, second);
   } catch (Exception e) {
     throw WrappedIOException.wrap("Caught exception processing input row ", e);
   }
 }
Esempio n. 4
1
 /**
  * Python % operator: y = n*x + z. The modulo operator always yields a result with the same sign
  * as its second operand (or zero). (Compare <code>java.Math.IEEEremainder</code>)
  *
  * @param x dividend
  * @param y divisor
  * @return <code>x % y</code>
  */
 private static double modulo(double x, double y) {
   if (y == 0.0) {
     throw Py.ZeroDivisionError("float modulo");
   } else {
     double z = x % y;
     if (z == 0.0) {
       // Has to be same sign as y (even when zero).
       return Math.copySign(z, y);
     } else if ((z > 0.0) == (y > 0.0)) {
       // z has same sign as y, as it must.
       return z;
     } else {
       // Note abs(z) < abs(y) and opposite sign.
       return z + y;
     }
   }
 }
Esempio n. 5
1
  @Override
  public Object visit(RealBinaryExpression n, Void arg) {

    Double leftDouble = (Double) n.getLeftOperand().accept(this, null);
    Double rightDouble = (Double) n.getRightOperand().accept(this, null);

    double leftVal = leftDouble.doubleValue();
    double rightVal = rightDouble.doubleValue();

    Operator op = n.getOperator();
    switch (op) {
      case DIV:
        return leftVal / rightVal;
      case MUL:
        return leftVal * rightVal;
      case MINUS:
        return leftVal - rightVal;
      case PLUS:
        return leftVal + rightVal;
      case REM:
        return leftVal % rightVal;
      case ATAN2:
        return Math.atan2(leftVal, rightVal);
      case COPYSIGN:
        return Math.copySign(leftVal, rightVal);
      case HYPOT:
        return Math.hypot(leftVal, rightVal);
      case IEEEREMAINDER:
        return Math.IEEEremainder(leftVal, rightVal);
      case MAX:
        return Math.max(leftVal, rightVal);
      case MIN:
        return Math.min(leftVal, rightVal);
      case NEXTAFTER:
        return Math.nextAfter(leftVal, rightVal);
      case POW:
        return Math.pow(leftVal, rightVal);
      case SCALB:
        return Math.scalb(leftVal, (int) rightVal);

      default:
        log.warn("IntegerBinaryExpression: unimplemented operator: " + op);
        return null;
    }
  }
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
      Log.d("Fling", "Fling with " + velocityX + ", " + velocityY);
      final View topCard = mTopCard;
      float dx = e2.getX() - e1.getX();
      if (Math.abs(dx) > mTouchSlop
          && Math.abs(velocityX) > Math.abs(velocityY)
          && Math.abs(velocityX) > mFlingSlop * 3) {
        float targetX = topCard.getX();
        float targetY = topCard.getY();
        long duration = 0;

        boundsRect.set(
            0 - topCard.getWidth() - 100,
            0 - topCard.getHeight() - 100,
            getWidth() + 100,
            getHeight() + 100);

        while (boundsRect.contains((int) targetX, (int) targetY)) {
          targetX += velocityX / 10;
          targetY += velocityY / 10;
          duration += 100;
        }

        duration = Math.min(500, duration);

        mTopCard = getChildAt(getChildCount() - 2);
        CardModel cardModel = (CardModel) getAdapter().getItem(getChildCount() - 1);

        if (mTopCard != null) mTopCard.setLayerType(LAYER_TYPE_HARDWARE, null);

        if (cardModel.getOnCardDismissedListener() != null) {
          if (targetX > 0) {
            cardModel.getOnCardDismissedListener().onLike();
          } else {
            cardModel.getOnCardDismissedListener().onDislike();
          }
        }

        topCard
            .animate()
            .setDuration(duration)
            .alpha(.75f)
            .setInterpolator(new LinearInterpolator())
            .x(targetX)
            .y(targetY)
            .rotation(Math.copySign(45, velocityX))
            .setListener(
                new AnimatorListenerAdapter() {
                  @Override
                  public void onAnimationEnd(Animator animation) {
                    removeViewInLayout(topCard);
                    ensureFull();
                  }

                  @Override
                  public void onAnimationCancel(Animator animation) {
                    onAnimationEnd(animation);
                  }
                });
        return true;
      } else return false;
    }
Esempio n. 7
1
  public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR && measuring) {
      SensorManager.getRotationMatrixFromVector(rMat, event.values);

      int newOrientation = getWindowManager().getDefaultDisplay().getRotation();

      if (newOrientation != oldOrientation) {

        switch (newOrientation) {
          case Surface.ROTATION_90:
            axisX = SensorManager.AXIS_X;
            axisY = SensorManager.AXIS_Y;
            break;

          case Surface.ROTATION_180:
            axisX = SensorManager.AXIS_Y;
            axisY = SensorManager.AXIS_MINUS_X;
            break;

          case Surface.ROTATION_270:
            axisX = SensorManager.AXIS_MINUS_X;
            axisY = SensorManager.AXIS_MINUS_Y;
            break;

          case Surface.ROTATION_0:
            axisX = SensorManager.AXIS_MINUS_Y;
            axisY = SensorManager.AXIS_X;
            break;

          default:
            break;
        }

        oldOrientation = newOrientation;
      }

      SensorManager.remapCoordinateSystem(rMat, axisX, axisY, mapped);

      x = (float) Math.sin(SensorManager.getOrientation(mapped, orientation)[1]);
      y = (float) Math.sin(SensorManager.getOrientation(mapped, orientation)[2]);

      r = (float) Math.hypot(x, y);
      theta = (float) Math.atan(y / x);

      if (r < .1) {
        if (!lit) {
          liquid.setImageResource(R.drawable.bubble_lit);
          lit = true;
        }
      } else {
        if (lit) {
          liquid.setImageResource(R.drawable.bubble_round);
          lit = false;
        }
      }

      if (r > 1) {
        x = (float) Math.copySign(Math.cos(theta), x);
        y = (float) Math.copySign(Math.sin(theta), y);
      }

      bubble.setLayoutParams(
          new AbsoluteLayout.LayoutParams(
              ball,
              ball,
              (int) (x * -size + size - ball / 2),
              (int) (y * -size + size - ball / 2)));
    }
  }