Search in sources :

Example 31 with StringMemory

use of php.runtime.memory.StringMemory in project jphp by jphp-compiler.

the class BCMathFunctions method bcmod.

public static Memory bcmod(Memory left, Memory modus) {
    BigDecimal base = toBigDecimal(left).setScale(0, RoundingMode.DOWN);
    BigDecimal mod = toBigDecimal(modus).setScale(0, RoundingMode.DOWN);
    if (mod.compareTo(BigDecimal.ZERO) == 0) {
        return Memory.NULL;
    }
    return new StringMemory(base.remainder(mod, MathContext.UNLIMITED).toString());
}
Also used : StringMemory(php.runtime.memory.StringMemory) BigDecimal(java.math.BigDecimal)

Example 32 with StringMemory

use of php.runtime.memory.StringMemory in project jphp by jphp-compiler.

the class BCMathFunctions method bcmul.

public static Memory bcmul(Environment env, Memory left, Memory right, int scale) {
    BigDecimal bd1 = toBigDecimal(left);
    BigDecimal bd2 = toBigDecimal(right);
    BigDecimal bd = bd1.multiply(bd2);
    // odd php special case for 0, scale is ignored:
    if (bd.compareTo(BigDecimal.ZERO) == 0) {
        if (scale > 0)
            return new StringMemory("0.0");
        else
            return new StringMemory("0");
    }
    bd = bd.setScale(scale, RoundingMode.DOWN);
    bd = bd.stripTrailingZeros();
    return new StringMemory(bd.toPlainString());
}
Also used : StringMemory(php.runtime.memory.StringMemory) BigDecimal(java.math.BigDecimal)

Example 33 with StringMemory

use of php.runtime.memory.StringMemory in project jphp by jphp-compiler.

the class BCMathFunctions method bcsqrt.

public static Memory bcsqrt(Environment env, Memory operand, int scale) {
    BigDecimal value = toBigDecimal(operand);
    int compareToZero = value.compareTo(BigDecimal.ZERO);
    if (compareToZero < 0) {
        return Memory.NULL;
    } else if (compareToZero == 0) {
        return new StringMemory("0");
    }
    int compareToOne = value.compareTo(BigDecimal.ONE);
    if (compareToOne == 0)
        return new StringMemory("1");
    int cscale;
    BigDecimal initialGuess;
    if (compareToOne < 1) {
        initialGuess = BigDecimal.ONE;
        cscale = value.scale();
    } else {
        BigInteger integerPart = value.toBigInteger();
        int length = integerPart.toString().length();
        if ((length % 2) == 0)
            length--;
        length /= 2;
        initialGuess = BigDecimal.ONE.movePointRight(length);
        cscale = Math.max(scale, value.scale()) + 2;
    }
    BigDecimal guess = initialGuess;
    BigDecimal lastGuess;
    for (int iteration = 0; iteration < 50; iteration++) {
        lastGuess = guess;
        guess = value.divide(guess, cscale, RoundingMode.DOWN);
        guess = guess.add(lastGuess);
        guess = guess.divide(DECIMAL_TWO, cscale, RoundingMode.DOWN);
        if (lastGuess.equals(guess)) {
            break;
        }
    }
    value = guess;
    value = value.setScale(scale, RoundingMode.DOWN);
    return new StringMemory(value.toPlainString());
}
Also used : StringMemory(php.runtime.memory.StringMemory) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal)

Example 34 with StringMemory

use of php.runtime.memory.StringMemory in project jphp by jphp-compiler.

the class ReflectionClass method setEntity.

public ReflectionClass setEntity(ClassEntity entity) {
    this.entity = entity;
    getProperties().put("name", new StringMemory(entity.getName()));
    return this;
}
Also used : StringMemory(php.runtime.memory.StringMemory)

Example 35 with StringMemory

use of php.runtime.memory.StringMemory in project jphp by jphp-compiler.

the class ReflectionExtension method setExtension.

public void setExtension(Extension extension) {
    this.extension = extension;
    getProperties().put("name", new StringMemory(extension.getName()));
}
Also used : StringMemory(php.runtime.memory.StringMemory)

Aggregations

StringMemory (php.runtime.memory.StringMemory)47 ArrayMemory (php.runtime.memory.ArrayMemory)14 Memory (php.runtime.Memory)11 ObjectMemory (php.runtime.memory.ObjectMemory)8 ReferenceMemory (php.runtime.memory.ReferenceMemory)7 BigDecimal (java.math.BigDecimal)5 Invoker (php.runtime.invoke.Invoker)4 ClassEntity (php.runtime.reflection.ClassEntity)4 TraceInfo (php.runtime.env.TraceInfo)3 IObject (php.runtime.lang.IObject)3 LongMemory (php.runtime.memory.LongMemory)3 StringWriter (java.io.StringWriter)2 ComponentProperties (org.develnext.jphp.swing.ComponentProperties)2 UIReader (org.develnext.jphp.swing.loader.UIReader)2 Signature (php.runtime.annotation.Reflection.Signature)2 CallStackItem (php.runtime.env.CallStackItem)2 ConcurrentEnvironment (php.runtime.env.ConcurrentEnvironment)2 CriticalException (php.runtime.exceptions.CriticalException)2 MethodEntity (php.runtime.reflection.MethodEntity)2 BigInteger (java.math.BigInteger)1