From a43af1394b1555b1e26f63a8fee27295dac18be5 Mon Sep 17 00:00:00 2001 From: bixb922 <70152274+bixb922@users.noreply.github.com> Date: Thu, 18 Apr 2024 16:08:58 -0400 Subject: [PATCH] Corrected small int/viper signed int range to -2**29 to 2**29-1 --- Improving-performance-with-Viper-code.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Improving-performance-with-Viper-code.md b/Improving-performance-with-Viper-code.md index 6cee895..aaaef81 100644 --- a/Improving-performance-with-Viper-code.md +++ b/Improving-performance-with-Viper-code.md @@ -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. ### 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. @@ -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. -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: ```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 -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): ```py