Processing, % (modulo)
用法介紹。
用法
value1 % value2
參數
value1
int 或浮點數value2
int 或浮點數
返回
int or float
說明
計算一個數除以另一個數時的餘數。例如,當 52.1 除以 10 時,除數 (10) 進入被除數 (52.1) 五次 (5 * 10 == 50),餘數為 2.1 (52.1 - 50 == 2.1)。因此,52.1 % 10 產生 2.1。
請注意,當除數大於被除數時,餘數構成整個被除數的值。也就是說,一個數不能除以任何比它自己大的數。例如,當 9 除以 10 時,結果為零,餘數為 9。因此,9 % 10 產生 9。
模數對於確保值保持在邊界內非常有用,例如在屏幕上保持形狀時。 (見上麵的第二個例子。)
例子
int a = 5 % 4; // Sets 'a' to 1
int b = 125 % 100; // Sets 'b' to 25
float c = 285.5 % 140.0; // Sets 'c' to 5.5
float d = 30.0 % 33.0; // Sets 'd' to 30.0
int a = 0;
void draw() {
background(200);
a = (a + 1) % width; // 'a' increases between 0 and width
line(a, 0, a, height);
}
有關的
相關用法
- Processing FFT用法及代碼示例
- Processing SawOsc.pan()用法及代碼示例
- Processing FloatDict用法及代碼示例
- Processing FFT.stop()用法及代碼示例
- Processing join()用法及代碼示例
- Processing () (parentheses)用法及代碼示例
- Processing Pulse用法及代碼示例
- Processing PShader用法及代碼示例
- Processing PVector.set()用法及代碼示例
- Processing FloatDict.sortKeysReverse()用法及代碼示例
- Processing texture()用法及代碼示例
- Processing IntDict.add()用法及代碼示例
- Processing PShape.enableStyle()用法及代碼示例
- Processing FloatDict.sub()用法及代碼示例
- Processing String用法及代碼示例
- Processing PImage.pixels[]用法及代碼示例
- Processing vertex()用法及代碼示例
- Processing PVector.mag()用法及代碼示例
- Processing thread()用法及代碼示例
- Processing Capture.stop()用法及代碼示例
- Processing Env.play()用法及代碼示例
- Processing StringList用法及代碼示例
- Processing parseJSONArray()用法及代碼示例
- Processing JSONArray.getIntArray()用法及代碼示例
- Processing Sound.inputDevice()用法及代碼示例
注:本文由純淨天空篩選整理自processing.org大神的英文原創作品 % (modulo)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。