modulo

How to calculate modulo of negative integers in JavaScript?

本秂侑毒 提交于 2019-12-08 14:45:59
问题 I'm trying to iterate over an array of jQuery objects, by incrementing or decrementing by 1. So, for the decrementing part, I use this code: var splitted_id = currentDiv.attr('id').split('_'); var indexOfDivToGo = parseInt(splitted_id[1]); indexOfDivToGo = (indexOfDivToGo-1) % allDivs.length; var divToGo = allDivs[indexOfDivToGo]; so I have 4 elements with id's: div_0 div_1 div_2 div_3 I was expecting it to iterate as 3 - 2 - 1 - 0 - 3 - 2 - etc.. but it returns -1 after the zero, therefore

How to implement floor modulo for every Number type in Kotlin?

本秂侑毒 提交于 2019-12-08 06:56:59
问题 I'm currently learning Kotlin and trying to create an extension (infix) method that works on all number types ( Byte , Long , Float , etc.). It should work like Python's % operator: 4 % 3 == 1 // only this is the same as Java's % 4 % -3 == -2 -4 % 3 == 2 -4 % -3 == -1 ...or like Java's Math.floorMod, but it should also work with Double or Float : -4.3 % 3.2 == 2.1000000000000005 or with any possible combination of these types 3 % 2.2 == 0.7999999999999998 3L % 2.2f == 0.7999999999999998 The

How to return the next letter in the alphabet of given letter wrapped around in Haskell [closed]

浪子不回头ぞ 提交于 2019-12-08 05:22:45
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I'm trying to implement a function that returns the next letter in alphabetical order. For example: > returnNext 'A' 'B' But also: > returnNext 'Z' 'A' The function should thus cycle between char codes in alphabetical order (mod 26). 回答1: Two ways come to mind import Data.Char returnNext c = chr (((1 + ord c -

How do I find the smallest positive integer congruent to i modulo m?

六眼飞鱼酱① 提交于 2019-12-07 20:18:11
问题 I have a variable that holds an angle, in degrees, which can be both positive and negative. I now need to make sure that this number is between 0 and 360 only. The number is a double. What would a nice algorithm for doing that be? Simply doing angle % 360 does not work, because negative numbers end up still being negative. Bonus points to the smallest algorithm (aka, Code Golf). EDIT Apparently this is different in different languages. In ActionScript and JavaScript, modulo will return a

Why different results of 0.5 mod 0.1 in different programming languages?

☆樱花仙子☆ 提交于 2019-12-07 14:11:47
问题 I have a question about modulo. The modulo operation finds the remainder of division of one number by another. I was expecting that the result of 0.5 % 0.1 = 0. But when I run this in PHP or .net I get 0.1. The code in php I ran was: var_dump(fmod(0.5, 0.1)); In .net I tried the following for the outcome: Console.WriteLine(0.5%0.1); I also tried an online calculator http://www.calculatorpro.com/modulo-calculator/. All these 3 methods gave me 0.1 as answer. But when I type this in google I get

If statement with modulo operator

♀尐吖头ヾ 提交于 2019-12-07 08:28:31
I tried this - x=[2,3,4,7,9] count=0 for i in x: if i%2: count=count+1 print count why the count is 3 instead of 2 , as i%2 is satusfiying only for "2 and 4"? The modulus of 2 over 2 is zero : >>> 2 % 2 0 So 2 % 2 produces 0 , which is a false value, and thus the if statement doesn't match. On the other hand, the modulus of 3 over to is one: >>> 3 % 2 1 1 is a non-zero integer, so considered true. In other words, the if i%2: test matches odd numbers, not even. There are 3 odd numbers in your list. Remember, modulus gives you the remainder of a division. 2 and 4 can be cleanly divided by 2, so

Getting a values most significant digit in Objective C

感情迁移 提交于 2019-12-07 06:43:08
问题 I currently have code in objective C that can pull out an integer's most significant digit value. My only question is if there is a better way to do it than with how I have provided below. It gets the job done, but it just feels like a cheap hack. What the code does is that it takes a number passed in and loops through until that number has been successfully divided to a certain value. The reason I am doing this is for an educational app that splits a number up by it's value and shows the

Java inverse modulo 2**64

☆樱花仙子☆ 提交于 2019-12-07 06:22:56
问题 Given an odd long x , I'm looking for long y such that their product modulo 2**64 (i.e., using the normal overflowing arithmetic) equals to 1. To make clear what I mean: This could be computed in a few thousand year this way: for (long y=1; ; y+=2) { if (x*y == 1) return y; } I know that this can be solved quickly using the extended Euclidean algorithm, but it requires the ability to represent all the involved numbers (ranging up to 2**64 , so even unsigned arithmetic wouldn't help). Using

Ruby modulo 3 with negative numbers is unintuitive [closed]

戏子无情 提交于 2019-12-07 05:48:10
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . Ruby modulo rules with negative numbers are unclear. In IRB: -7 % 3 == 2 Should be 1 ! Why? 回答1: Because -7/3 is -3 under Ruby's integer division semantics. 3*-3 is -9, so that would leave a remainder of 2.

C#: decrementing a clock using modulus math

元气小坏坏 提交于 2019-12-07 03:46:29
问题 Trying to emulate the rollover of a 24 hour clock by hand (with math vs. using the timespan classes). The incrementing part was easy to figure out how to roll over from 23:00 to 0:00 and from, but getting it to go the other way is turning out to be really confusing. Here's what I have so far: static void IncrementMinute(int min, int incr) { int newMin = min + incr, hourIncrement = newMin / 60; //increment or decrement the hour if((double)newMin % 60 < 0 && (double)newMin % 60 > -1)