Skip to content

Commit 95c99dd

Browse files
committed
doc: toNat behavior
1 parent 307fb67 commit 95c99dd

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/Init/Data/SInt/Basic.lean

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ abbrev Int.toInt8 := Int8.ofInt
6868
abbrev Nat.toInt8 := Int8.ofNat
6969
@[extern "lean_int8_to_int"]
7070
def Int8.toInt (i : Int8) : Int := i.toBitVec.toInt
71+
/--
72+
This function has the same behavior as `Int.toNat` for negative numbers.
73+
If you want to obtain the 2's complement representation use `toBitVec`.
74+
-/
7175
@[inline] def Int8.toNat (i : Int8) : Nat := i.toInt.toNat
7276
@[extern "lean_int8_neg"]
7377
def Int8.neg (i : Int8) : Int8 := ⟨⟨-i.toBitVec⟩⟩
@@ -161,6 +165,10 @@ abbrev Int.toInt16 := Int16.ofInt
161165
abbrev Nat.toInt16 := Int16.ofNat
162166
@[extern "lean_int16_to_int"]
163167
def Int16.toInt (i : Int16) : Int := i.toBitVec.toInt
168+
/--
169+
This function has the same behavior as `Int.toNat` for negative numbers.
170+
If you want to obtain the 2's complement representation use `toBitVec`.
171+
-/
164172
@[inline] def Int16.toNat (i : Int16) : Nat := i.toInt.toNat
165173
@[extern "lean_int16_to_int8"]
166174
def Int16.toInt8 (a : Int16) : Int8 := ⟨⟨a.toBitVec.signExtend 8⟩⟩
@@ -258,6 +266,10 @@ abbrev Int.toInt32 := Int32.ofInt
258266
abbrev Nat.toInt32 := Int32.ofNat
259267
@[extern "lean_int32_to_int"]
260268
def Int32.toInt (i : Int32) : Int := i.toBitVec.toInt
269+
/--
270+
This function has the same behavior as `Int.toNat` for negative numbers.
271+
If you want to obtain the 2's complement representation use `toBitVec`.
272+
-/
261273
@[inline] def Int32.toNat (i : Int32) : Nat := i.toInt.toNat
262274
@[extern "lean_int32_to_int8"]
263275
def Int32.toInt8 (a : Int32) : Int8 := ⟨⟨a.toBitVec.signExtend 8⟩⟩
@@ -359,6 +371,10 @@ abbrev Int.toInt64 := Int64.ofInt
359371
abbrev Nat.toInt64 := Int64.ofNat
360372
@[extern "lean_int64_to_int_sint"]
361373
def Int64.toInt (i : Int64) : Int := i.toBitVec.toInt
374+
/--
375+
This function has the same behavior as `Int.toNat` for negative numbers.
376+
If you want to obtain the 2's complement representation use `toBitVec`.
377+
-/
362378
@[inline] def Int64.toNat (i : Int64) : Nat := i.toInt.toNat
363379
@[extern "lean_int64_to_int8"]
364380
def Int64.toInt8 (a : Int64) : Int8 := ⟨⟨a.toBitVec.signExtend 8⟩⟩

0 commit comments

Comments
 (0)