Mechatronics LAB
Published © GPL3+

Arduino and MicroPython Floating-Point Numbers Sketch

Arduino and MicroPython Floating-Point Numbers Sketch in ESP32

BeginnerProtip1 hour46
Arduino and MicroPython Floating-Point Numbers Sketch

Things used in this project

Hardware components

ESP32 Development Board
×1
Jumper Wires
×1
Breadboard
×1

Story

Read more

Code

Arduino code

Arduino
import time
import math

value = 1.1

def almost_equal(a, b, delta=0.00001):
    if a == 0:
        return abs(b) <= delta
    if b == 0:
        return abs(a) <= delta
    return abs((a - b) / max(abs(a), abs(b))) <= delta

while True:
    value -= 0.1
    
    if value == 0:
        print("The value is exactly zero")
    elif almost_equal(value, 0):
        print(f"The value {value:.7f} is almost equal to zero, restarting countdown")
        value = 1.1
    else:
        print(value)
    
    time.sleep(0.25)

MicroPython Floating-Point Numbers-

Python
import time
import math

value = 1.1

def almost_equal(a, b, delta=0.00001):
    if a == 0:
        return abs(b) <= delta
    if b == 0:
        return abs(a) <= delta
    return abs((a - b) / max(abs(a), abs(b))) <= delta

while True:
    value -= 0.1
    
    if value == 0:
        print("The value is exactly zero")
    elif almost_equal(value, 0):
        print(f"The value {value:.7f} is almost equal to zero, restarting countdown")
        value = 1.1
    else:
        print(value)
    
    time.sleep(0.25)

Credits

Mechatronics LAB
75 projects • 49 followers
I am Sarful , I am a Mechatronics Engineer & also a teacher I am Interested in the evolution of technology in the automation industry .

Comments