⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fix.java

📁 JAVA 数学程序库 提供常规的数值计算程序包
💻 JAVA
字号:
package jmathlib.toolbox.general;

import jmathlib.core.tokens.numbertokens.DoubleNumberToken;
import jmathlib.core.tokens.Token;
import jmathlib.core.tokens.OperandToken;
import jmathlib.core.functions.ExternalFunction;


public class fix extends ExternalFunction
{
	/**rounds the element to the nearest element towards zero.
	@param operands[0] = value to round
	@return a matrix of the same size as the operands*/
	public OperandToken evaluate(Token[] operands)
	{
		
        // exactly one operand
        if (getNArgIn(operands) != 1)
			throwMathLibException("fix: number of arguments != 1");
        
		// only works on numbers
		if(!(operands[0] instanceof DoubleNumberToken))
			throwMathLibException("fix: only works on numbers");
		
		DoubleNumberToken matrix = ((DoubleNumberToken)operands[0]);
		OperandToken temp  = ((OperandToken)matrix.clone());
					
		double[][] reValues = matrix.getValuesRe();
		double[][] imValues = matrix.getValuesIm();
		for(int y = 0; y < matrix.getSizeY(); y++)
		{
			for(int x = 0; x < matrix.getSizeX(); x++)
			{
				if(reValues[y][x] >=0 )
				{	
					// e.g. fix(3.2) => 3
					reValues[y][x] = java.lang.Math.floor(reValues[y][x]);
				}
				else
				{
					// e.g. fix(-3.2) => -3
					reValues[y][x] = java.lang.Math.ceil(reValues[y][x]);
				}
				if(imValues[y][x] >=0 )
				{	
					// e.g. fix(3.2i) => 3
					imValues[y][x] = java.lang.Math.floor(imValues[y][x]);
				}
				else
				{
					// e.g. fix(-3.2i) => -3
					imValues[y][x] = java.lang.Math.ceil(imValues[y][x]);
				}
			}
		}
		return new DoubleNumberToken(reValues, imValues);
		
	}	
}

/*
@GROUP
general
@SYNTAX
answer=fix(matrix)
@DOC
Rounds the element to the nearest element towards zero.
@EXAMPLES
<programlisting>
fix(2.3)=3  
fix(-2.3)=-2 
fix(1.9+2.4i)=1+2i 
fix(1.9-2.4i)=1-2i 
</programlisting>
@NOTES
@SEE
ceil, round, floor
**/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -