github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/examples/gno.land/p/demo/rat/rat.gno (about) 1 package rat 2 3 //---------------------------------------- 4 // Rat fractions 5 6 // represents a fraction. 7 type Rat struct { 8 X int32 9 Y int32 // must be positive 10 } 11 12 func NewRat(x, y int32) Rat { 13 if y <= 0 { 14 panic("invalid std.Rat denominator") 15 } 16 return Rat{X: x, Y: y} 17 } 18 19 func (r1 Rat) IsValid() bool { 20 if r1.Y <= 0 { 21 return false 22 } 23 return true 24 } 25 26 func (r1 Rat) Cmp(r2 Rat) int { 27 if !r1.IsValid() { 28 panic("invalid std.Rat left operand") 29 } 30 if !r2.IsValid() { 31 panic("invalid std.Rat right operand") 32 } 33 var p1, p2 int64 34 p1 = int64(r1.X) * int64(r2.Y) 35 p2 = int64(r1.Y) * int64(r2.X) 36 if p1 < p2 { 37 return -1 38 } else if p1 == p2 { 39 return 0 40 } else { 41 return 1 42 } 43 } 44 45 //func (r1 Rat) Plus(r2 Rat) Rat { 46 // XXX 47 //}