modulus

Modulus with doubles in Java

最后都变了- 提交于 2019-11-27 14:06:13
How do you deal with Java's weird behaviour with the modulus operator when using doubles? For example, you would expect the result of 3.9 - (3.9 % 0.1) to be 3.9 (and indeed, Google says I'm not going crazy), but when I run it in Java I get 3.8000000000000003 . I understand this is a result of how Java stores and processes doubles, but is there a way to work around it? Use a precise type if you need a precise result: double val = 3.9 - (3.9 % 0.1); System.out.println(val); // 3.8000000000000003 BigDecimal x = new BigDecimal( "3.9" ); BigDecimal bdVal = x.subtract( x.remainder( new BigDecimal(

Reverse Modulus Operator

北城余情 提交于 2019-11-27 12:03:00
问题 Over 3 years after asking the question I found the solution. I have included it as an answer. I have an expression with modulus in it that needs to be put in terms of x. (a + x) mod m = b I can't figure out what to do with the modulus. Is there a way to get x by itself, or am I out of luck on this one? Edit : I realize that I can get multiple answers, but I'm looking for an answer that falls within the range of m. 回答1: I was revisiting this question and realized it is possible based off of

What does “% is unavailable: Use truncatingRemainder instead” mean?

心不动则不痛 提交于 2019-11-27 09:50:02
问题 I get the following error when using code for an extension, I'm not sure if they're asking to just use a different operator or modify the values in the expression based on an internet search. Error: % is unavailable: Use truncatingRemainder instead Extension code: extension CMTime { var durationText:String { let totalSeconds = CMTimeGetSeconds(self) let hours:Int = Int(totalSeconds / 3600) let minutes:Int = Int(totalSeconds % 3600 / 60) let seconds:Int = Int(totalSeconds % 60) if hours > 0 {

How does Python implement the modulo operation?

这一生的挚爱 提交于 2019-11-27 07:48:18
问题 I'm curious in regards to the time and space complexities of the % operator in Python. Also, does Python use a bitwise operation for % 2 ? Edit: I'm asking about Python 2.7's implementation, just in case it differs slightly from that of Python 3 回答1: Python uses the classic Algorithm D from Knuth's 'The Art of Computer Programming'. The running time is (generally) proportional to the product of lengths of the two numbers. Space is proportional to the sum of the lengths of the two numbers. The

How do I use modulus for float/double?

橙三吉。 提交于 2019-11-27 03:52:28
I'm creating an RPN calculator for a school project. I'm having trouble with the modulus operator. Since we're using the double data type, modulus won't work on floating point numbers. For example, 0.5 % 0.3 should return 0.2 but I'm getting a division by zero exception. The instruction says to use fmod(). I've looked everywhere for fmod(), including javadocs but I can't find it. I'm starting to think it's a method I'm going to have to create? edit: hmm, strange. I just plugged in those numbers again and it seems to be working fine...but just in case. Do I need to watch out using the mod

php modulus in a loop

陌路散爱 提交于 2019-11-27 02:08:35
I'm currently checking whether an entry in a loop is the 3rd iteration or not, with the following code: <?php for ($i = 0; $i < count($category_news); $i++) : ?> <div class="grid_8"> <div class="candidate snippet <?php if ($i % 3 == 2) echo "end"; ?>"> <div class="image shadow_50"> <img src="<?php echo base_url();?>media/uploads/news/<?php echo $category_news[$i]['url']; ?>" alt="Image Preview" width="70px" height="70px"/> </div> <h5><?php echo $category_news[$i]['title']?></h5> <p><?php echo strip_tags(word_limiter($category_news[$i]['article'], 15)); ?></p> <?php echo anchor('/news/article

Find multiples of a number in PHP

北战南征 提交于 2019-11-27 01:31:47
问题 I want to find all muliples of a number in PHP. I'm using something like this if($count != 20 ) to work out if $count is not equal to 20. But I also need this script to check if $count is not equal to 20, 40, 60, 80, 100, 120, 140, 160 etc. Any ideas? I think i need to use the modulus symbol ( % ), but I don't know. 回答1: if ($count % 20 != 0) 回答2: if ($count % 20 != 0) { // $count is not a multiple of 20 } 回答3: If you don't want zero to be excluded: if ($count % 20 != 0 || $count == 0) 回答4:

Selecting rows where remainder (modulo) is 1 after division by 2?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 00:40:57
问题 There is a column in options that hold an integer. I want to select the row only if that value % 2 = 1. I know this can be done in 2 queries but is it possible to do it in 1? 回答1: MySQL, SQL Server, PostgreSQL, SQLite support using the percent sign as the modulus: WHERE column % 2 = 1 For Oracle, you have to use the MOD function: WHERE MOD(column, 2) = 1 回答2: At least some versions of SQL (Oracle, Informix, DB2, ISO Standard) support: WHERE MOD(value, 2) = 1 MySQL supports '%' as the modulus

Why does modulus operator return fractional number in javascript?

删除回忆录丶 提交于 2019-11-26 19:00:55
Why does 49.90 % 0.10 in JavaScript return 0.09999999999999581 ? I expected it to be 0. Because JavaScript uses floating point math which always leads to rounding errors. If you need an exact result with two decimal places, multiply your numbers with 100 before the operation and then divide again afterwards: var result = ( 4990 % 10 ) / 100; Round if necessary. kennytm Javascript's Number is using "IEEE double-precision" to store the values. They are incapable of storing all decimal numbers exactly. The result is not zero because of round-off error when converting the decimal number to binary.

Modulus power of big numbers

核能气质少年 提交于 2019-11-26 17:53:20
I am trying to implement the SAFER+ algorithm. The algorithm requires finding the modulus of a power function as follows: pow(45, x) mod 257 The variable x is a byte, and thus can range from 0 to 255. Accordingly, the result of the power function can be VERY big resulting in incorrect values if implemented using 32- or 64-bit integers. How can I perform this calculation? some pseudo code function powermod(base, exponent, modulus) { if (base < 1 || exponent < 0 || modulus < 1) return -1 result = 1; while (exponent > 0) { if ((exponent % 2) == 1) { result = (result * base) % modulus; } base =