Corrected small int/viper signed int range to -2**29 to 2**29-1

master
bixb922 2024-04-18 16:08:58 -04:00
rodzic 6fdc19f011
commit a43af1394b
1 zmienionych plików z 3 dodań i 3 usunięć

@ -116,7 +116,7 @@ A viper `int` is different to the ```int``` we know in MicroPython, which is sti
It is advisable to be aware at all times that `viper int` and `builtins.int` are different data types. It is advisable to be aware at all times that `viper int` and `builtins.int` are different data types.
### Viper integer constants ### Viper integer constants
Viper integer constants are in the range -2\*\*30 to 2\*\*30-1. When you assign a viper constant to a variable, it automatically is a viper `int`. Viper integer constants are in the range -2\*\*29 to 2\*\*29-1. When you assign a viper constant to a variable, it automatically is a viper `int`.
Be aware: integer constants don't have the full range of values a viper int value can hold, they are signed 31 bit integers. Be aware: integer constants don't have the full range of values a viper int value can hold, they are signed 31 bit integers.
@ -133,7 +133,7 @@ As it is usual in Python, a viper variable is of type viper `int when you assig
``` ```
If the variable is created by assigning an expression, the viper code emitter will evaluate the expression at compile time. If the variable is created by assigning an expression, the viper code emitter will evaluate the expression at compile time.
Be aware: Integer expressions outside what is called the "small integer" range of MicroPython are not viper `int` but `builtins.int`. On most architectures a MicroPython small integer falls is -2\*\*30-1 and 2\*\*30-1. Be aware: Integer expressions outside what is called the "small integer" range of MicroPython are not viper `int` but `builtins.int`. On most architectures a MicroPython small integer falls is -2\*\*29 and 2\*\*29-1.
For example: For example:
```py ```py
@ -156,7 +156,7 @@ With the type hint, `x` is converted on the fly to the viper `int` data type usi
## Making sure a viper int is a viper int ## Making sure a viper int is a viper int
There is a possible source of problems: when you initialize a viper `int` with a integer expression that falls outside of the signed 30 bit range (not the 32 bit range!), a `builtins.int` will be created instead, no warning. The same happens if you try initialize a viper int with a variable of type `builtins.int`. These errors can go unnoticed. There is a possible source of problems: when you initialize a viper `int` with a integer expression that falls outside of the viper int range (which is not the 32 bit range!), a `builtins.int` will be created instead, no warning. The same happens if you try initialize a viper int with a variable of type `builtins.int`. These errors can go unnoticed.
Solution: Except for very short viper functions, you could initialize all viper `int` variables at the beginning setting them to zero (just as you might do in C language): Solution: Except for very short viper functions, you could initialize all viper `int` variables at the beginning setting them to zero (just as you might do in C language):
```py ```py