Python Forum
Add two number and variable datatype is int8
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add two number and variable datatype is int8
#1
Hi,
Write a simple python program to add two numbers and variables datatype should be int8.
How can I do this ?

Actually by default variables take only 'int' here I am confused.and after getting your reply,
I will put value in variable of int8 datatype to check range -128 to 127. Ex, val1 = 130
and error should be come due to out of range , I think.
Reply
#2
Is it allowed for you to use NumPy? Numpy has int8 data type.
Reply
#3
(Aug-05-2019, 01:35 AM)scidam Wrote: Is it allowed for you to use NumPy? Numpy has int8 data type.

how can we do it without numpy ?
Reply
#4
import numpy as np


value_int8 = np.int8(127)
value_uint8 = np.uint8(255)
Or you make your own type in Python:

class Int8(int):
    def __init__(self, value):
        if not -128 <= value <= 127:
            raise ValueError('-128 <= int8 <= 127')
But this still allows to get bigger or smaller values than allowed, after a calculation.
If there is a limit, just use if-statements to check if the value is in range.

If you need to be flexible with size, you can make a validator closure (function in a function).

def in_range(min_val, max_val):
    """
    Closure to create a new function to check
    if the given value is in range.
    """
    def check(value):
        # min_val and max_val is from the first call
        return min_val <= value <= max_val # <- this is the second call
    return check # <- returns the inner function, without calling it


def in_range_exc(min_val, max_val):
    """
    Closure to create a new function to check
    if the given value is in range. If not,
    a ValueError is raised.
    """
    def check(value):
        if not min_val <= value <= max_val:
            raise ValueError(f'{min_val} <= {value} <= {max_val}')
    return check


voltage_ok = in_range(-10, +10)
current_ok = in_range(-0.02, +0.02)
power_ok = in_range(10, 11)

print(voltage_ok(0))
print(voltage_ok(1))
print(voltage_ok(10))
print(voltage_ok(-10))
print(voltage_ok(-10.00000000000000000000000001)) # < this is True, try to find out why
print(voltage_ok(-10.001))

voltage_ok_exc = in_range_exc(5, 5.1)

current_voltage = 5.10001
try:
    voltage_ok_exc(current_voltage)
except ValueError:
    print(f'The voltage {current_voltage}V is not in range')
Usually you need this data types (int8, int16 ...) if you want to interact with C.
In this case ctypes is the right module for it.

If you want to handle data over network/serial or in general binary data, you sould look for the struct and binascii module.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to change the datatype of list elements? mHosseinDS86 9 1,952 Aug-24-2022, 05:26 PM
Last Post: deanhystad
  Give a number for Variable quest 2 1,500 Jan-31-2022, 08:35 AM
Last Post: ibreeden
  datatype check arkiboys 1 1,174 Jan-18-2022, 12:46 PM
Last Post: ndc85430
  [solved] Variable number of dictionnaries as argument in def() paul18fr 11 6,106 Apr-20-2021, 11:15 AM
Last Post: paul18fr
  Error when Excelwriter saving a dataframe with datetime datatype with timezone klllmmm 3 13,321 Dec-08-2020, 11:37 AM
Last Post: Larz60+
  [regex] Good way to parse variable number of items? Winfried 4 2,607 May-15-2020, 01:54 PM
Last Post: Winfried
  I need to get only string datatype and report in excel file. akshay3210 3 2,361 Dec-12-2019, 09:53 AM
Last Post: akshay3210
  Unable to read decimal datatype using pandasql geethchi 0 2,035 Oct-25-2019, 05:56 PM
Last Post: geethchi
  joining a variable number of lists in a comprehension Skaperen 2 2,803 Oct-14-2017, 08:19 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020