Files
Jam/src/tools.py
2023-05-10 15:28:22 -04:00

46 lines
1.2 KiB
Python

# Copyright 2021-2023 Ian Jauslin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import math
# sign function
def sgn(x):
if x>=0:
return 1
return -1
# check whether a number is an integer (with tolerance)
def isint_nonzero(x):
if abs(x)<1e-11:
return False
return abs(round(x)-x)<1e-11
# check that a number is in an interval
def in_interval(x,a,b):
return x>=a and x<=b
# remove x from list a
def remove_fromlist(a,x):
if x in a:
a[a.index(x)]=a[len(a)-1]
a=a[:len(a)-1]
return a
# snap to a grid: ceiling
def ceil_grid(x,size):
return math.ceil(x/size)*size
# snap to a grid: floor
def floor_grid(x,size):
return math.floor(x/size)*size