Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
F
firmware
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Monitor
Service Desk
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
card10
firmware
Commits
eee6e334
Verified
Commit
eee6e334
authored
5 years ago
by
rahix
Browse files
Options
Downloads
Patches
Plain Diff
doc: Document MicroPython integers
Signed-off-by:
Rahix
<
rahix@rahix.de
>
parent
64b73df5
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Documentation/pycardium-guide.rst
+49
-2
49 additions, 2 deletions
Documentation/pycardium-guide.rst
with
49 additions
and
2 deletions
Documentation/pycardium-guide.rst
+
49
−
2
View file @
eee6e334
...
...
@@ -234,9 +234,56 @@ Booleans
Integers
~~~~~~~~
.. todo::
As long as your integers stay within **31**-bit limits, integers are stored very
efficiently and can be accessed and created like this:
.. code-block:: cpp
#include "py/obj.h"
/* Create a new integer which is < 2^31 */
mp_obj_t int_obj = MP_OBJ_NEW_SMALL_INT(0xc0ffee);
/* Check if an integer is small and if so, extract it */
if (mp_obj_is_small_int(int_obj)) {
int int_value = MP_OBJ_SMALL_INT_VALUE(int_obj);
}
For bigger integers or if you are uncertain about the limits, use the following
functions:
.. code-block:: cpp
#include "py/obj.h"
/* Create new integer objects in various sizes and signedness */
mp_obj_t int0_obj = mp_obj_new_int((mp_int_t)value);
mp_obj_t int1_obj = mp_obj_new_int_from_uint((mp_uint_t)value);
mp_obj_t int2_obj = mp_obj_new_int_from_ll((long long)value);
mp_obj_t int3_obj = mp_obj_new_int_from_ull((unsigned long long)value);
/* Check if a value is an integer */
if (mp_obj_is_integer(int_obj)) {
/* Get an integer */
int int0 = mp_obj_get_int(int_obj);
int int0 = mp_obj_get_int_truncated(int_obj);
}
int value;
if (!mp_obj_get_int_maybe(int_obj, &value)) {
/* Not an integer! */
}
To get really big integers you have to use
.. code-block:: cpp
#include "py/obj.h"
#include "py/objint.h"
Integers
long long value;
mp_obj_int_to_bytes_impl(int_obj, 8, (byte*)&value);
Strings
~~~~~~~
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment