1# test ffi float support 2try: 3 import ffi 4except ImportError: 5 print("SKIP") 6 raise SystemExit 7 8 9def ffi_open(names): 10 err = None 11 for n in names: 12 try: 13 mod = ffi.open(n) 14 return mod 15 except OSError as e: 16 err = e 17 raise err 18 19 20libc = ffi_open(("libc.so", "libc.so.0", "libc.so.6", "libc.dylib")) 21 22try: 23 strtof = libc.func("f", "strtof", "sp") 24except OSError: 25 # Some libc's (e.g. Android's Bionic) define strtof as macro/inline func 26 # in terms of strtod(). 27 print("SKIP") 28 raise SystemExit 29 30print("%.6f" % strtof("1.23", None)) 31 32strtod = libc.func("d", "strtod", "sp") 33print("%.6f" % strtod("1.23", None)) 34 35# test passing double and float args 36libm = ffi_open(("libm.so", "libm.so.6", "libc.so.0", "libc.so.6", "libc.dylib")) 37tgamma = libm.func("d", "tgamma", "d") 38for fun in (tgamma,): 39 for val in (0.5, 1, 1.0, 1.5, 4, 4.0): 40 print("%.6f" % fun(val)) 41