mod

Mod with negative numbers gives a negative result in Java and C

谁说我不能喝 提交于 2019-12-06 16:03:11
问题 Let's say I have (-5) mod 8 . I tried it in both languages Java and C, and they gave me a -5 result when I was expecting 3 . Why is this happening? Can a modulus be negative? And what should I change to get the correct result? Java code public class Example { public static void main(String[] args) { int x; x = -5%8; System.out.println(x); } } C code int main(){ int x; x = -5%8; printf("%d", x); } OUTPUTS 回答1: The % operator is treated as a remainder operator, so the sign of the result is the

Reuse components in angular2 model driven forms

时光总嘲笑我的痴心妄想 提交于 2019-11-30 13:17:16
I'm fairly new to angular2 and for the past few days I have been trying to create reusable form components using model driven forms So lets say we have a component componentA.component.ts @Component({ selector: 'common-a', template: ` <div [formGroup]="_metadataIdentifier"> <div class="form-group"> <label>Common A[1]</label> <div> <input type="text" formControlName="valueA1"> <small>Description 1</small> </div> <div class="form-group"> <label>Common A[2]</label> <div> <input type="text" formControlName="valueA2"> <small>Description 2</small> </div> </div> ` }) export class ComponentA

Mod in Java produces negative numbers [duplicate]

隐身守侯 提交于 2019-11-26 03:32:45
问题 This question already has answers here : Best way to make Java's modulus behave like it should with negative numbers? (6 answers) Closed 2 years ago . When I calculate int i = -1 % 2 I get -1 in Java. In Python, I get 1 as the result of -1 % 2 . What do I have to do to get the same behavior in Java with the modulo function? 回答1: The problem here is that in Python the % operator returns the modulus and in Java it returns the remainder . These functions give the same values for positive