Skip to main content
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

PowerShell, 38 31 bytes

$a,$b=$args;($a-$a%$b)/$b;$a%$b

Try it online!

Sheesh, this is icky. So, PowerShell (helpfully) returns floating point values when doing division if it doesn't divide evenly. Sometimes, this is a Good Thing, but other times (like here) it's very not. So, you'd figure "Oh, let's just toss an [int] cast and call it good, right?" Nope. Casting from a [double] to an [int] in PowerShell does banker's roundingbanker's rounding, so for input 5, 7 we would get 1 back, not 0. As a result, we need to subtract the remainder (from the modulo), then calculate the division, and then calculate the modulo again. Yay!

Both results are left on the pipeline, and output is implicit.

Saved 7 bytes thanks to PeterTaylor being smarter than me.

PowerShell, 38 31 bytes

$a,$b=$args;($a-$a%$b)/$b;$a%$b

Try it online!

Sheesh, this is icky. So, PowerShell (helpfully) returns floating point values when doing division if it doesn't divide evenly. Sometimes, this is a Good Thing, but other times (like here) it's very not. So, you'd figure "Oh, let's just toss an [int] cast and call it good, right?" Nope. Casting from a [double] to an [int] in PowerShell does banker's rounding, so for input 5, 7 we would get 1 back, not 0. As a result, we need to subtract the remainder (from the modulo), then calculate the division, and then calculate the modulo again. Yay!

Both results are left on the pipeline, and output is implicit.

Saved 7 bytes thanks to PeterTaylor being smarter than me.

PowerShell, 38 31 bytes

$a,$b=$args;($a-$a%$b)/$b;$a%$b

Try it online!

Sheesh, this is icky. So, PowerShell (helpfully) returns floating point values when doing division if it doesn't divide evenly. Sometimes, this is a Good Thing, but other times (like here) it's very not. So, you'd figure "Oh, let's just toss an [int] cast and call it good, right?" Nope. Casting from a [double] to an [int] in PowerShell does banker's rounding, so for input 5, 7 we would get 1 back, not 0. As a result, we need to subtract the remainder (from the modulo), then calculate the division, and then calculate the modulo again. Yay!

Both results are left on the pipeline, and output is implicit.

Saved 7 bytes thanks to PeterTaylor being smarter than me.

Saved 7 bytes thanks to PeterTaylor being smarter than me
Source Link
AdmBorkBork
  • 43.5k
  • 5
  • 103
  • 284

PowerShell, 3838 31 bytes

$a,$b=$args;[math]::floor$b=$args;($a/$b-$a%$b);$a%$b/$b;$a%$b

Try it online!Try it online!

Sheesh, this is icky. So, PowerShell (helpfully) returns floating point values when doing division if it doesn't divide evenly. Sometimes, this is a Good Thing, but other times (like here) it's very not. So, you'd figure "Oh, let's just toss an [int] cast and call it good, right?" Nope. Casting from a [double] to an [int] in PowerShell does banker's rounding, so for input 5, 7 we would get 1 back, not 0. As a result, we need to subtract the remainder [math]::floor()(from the modulo), then calculate the division result to get a proper integer back, and then calculate the modulo again. Yay!

The modulo is just standard % like other languages.

Both results are left on the pipeline, and output is implicit.

Saved 7 bytes thanks to PeterTaylor being smarter than me.

PowerShell, 38 bytes

$a,$b=$args;[math]::floor($a/$b);$a%$b

Try it online!

Sheesh, this is icky. So, PowerShell (helpfully) returns floating point values when doing division if it doesn't divide evenly. Sometimes, this is a Good Thing, but other times (like here) it's very not. So, you'd figure "Oh, let's just toss an [int] cast and call it good, right?" Nope. Casting from a [double] to an [int] in PowerShell does banker's rounding, so for input 5, 7 we would get 1 back, not 0. As a result, we need to [math]::floor() the division result to get a proper integer back. Yay!

The modulo is just standard % like other languages.

Both results are left on the pipeline, and output is implicit.

PowerShell, 38 31 bytes

$a,$b=$args;($a-$a%$b)/$b;$a%$b

Try it online!

Sheesh, this is icky. So, PowerShell (helpfully) returns floating point values when doing division if it doesn't divide evenly. Sometimes, this is a Good Thing, but other times (like here) it's very not. So, you'd figure "Oh, let's just toss an [int] cast and call it good, right?" Nope. Casting from a [double] to an [int] in PowerShell does banker's rounding, so for input 5, 7 we would get 1 back, not 0. As a result, we need to subtract the remainder (from the modulo), then calculate the division, and then calculate the modulo again. Yay!

Both results are left on the pipeline, and output is implicit.

Saved 7 bytes thanks to PeterTaylor being smarter than me.

Source Link
AdmBorkBork
  • 43.5k
  • 5
  • 103
  • 284

PowerShell, 38 bytes

$a,$b=$args;[math]::floor($a/$b);$a%$b

Try it online!

Sheesh, this is icky. So, PowerShell (helpfully) returns floating point values when doing division if it doesn't divide evenly. Sometimes, this is a Good Thing, but other times (like here) it's very not. So, you'd figure "Oh, let's just toss an [int] cast and call it good, right?" Nope. Casting from a [double] to an [int] in PowerShell does banker's rounding, so for input 5, 7 we would get 1 back, not 0. As a result, we need to [math]::floor() the division result to get a proper integer back. Yay!

The modulo is just standard % like other languages.

Both results are left on the pipeline, and output is implicit.