Python Forum
math.remainder(a, b) question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
math.remainder(a, b) question
#2
Read the docs

https://docs.python.org/3/library/math.html

Quote:math.remainder(x, y)¶
Return the IEEE 754-style remainder of x with respect to y. For finite x and finite nonzero y, this is the difference x - n*y, where n is the closest integer to the exact value of the quotient x / y. If x / y is exactly halfway between two consecutive integers, the nearest even integer is used for n. The remainder r = remainder(x, y) thus always satisfies abs® <= 0.5 * abs(y).

According to that, remainder(3, 2) == -1. also 11, 15 or any 2*n - 1 where n is even. Half the time.

You could use the modulo operator (%)

https://docs.python.org/3.3/reference/expressions.html

Quote:The % (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the ZeroDivisionError exception. The arguments may be floating point numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 4*0.7 + 0.34.) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand [1].
print(7 % 2, math.remainder(7, 2))
Output:
1 -1.0
Hudjefa likes this post
Reply


Messages In This Thread
math.remainder(a, b) question - by Hudjefa - Sep-08-2024, 07:46 AM
RE: math.remainder(a, b) question - by deanhystad - Sep-08-2024, 08:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Math python question Deonvek 6 2,307 Apr-05-2023, 09:27 PM
Last Post: deanhystad
  math.log versus math.log10 stevendaprano 10 4,474 May-23-2022, 08:59 PM
Last Post: jefsummers
  Why getting ValueError : Math domain error in trig. function, math.asin() ? jahuja73 3 5,231 Feb-24-2021, 05:09 PM
Last Post: bowlofred
  Converting a remainder to int to str tester_V 6 3,912 Oct-28-2020, 06:14 PM
Last Post: tester_V
  Could not parse the remainder: '=' from '=' Saurabh 3 16,372 Feb-20-2019, 11:18 AM
Last Post: buran
  remainder operator % noweare 3 3,502 Feb-14-2019, 01:50 AM
Last Post: noweare

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020