1. bigatom.e - New ba_log10() function (fast)
- Posted by cargoan Dec 23, 2014
- 1194 views
-- con atajo para números grandes -- log10(x * y) = log10(x) + log10(y) -- y si 'x' ó 'y' son una potencia de diez, su logaritmo decimal es el -- exponente y basta con sumarlo al logaritmo del otro número con exponente 0 -- así no es un número grande y se calcula más rápido... -- ...el logaritmo de un número pequeño y una suma -- me sorprende no haber caído antes... tan simple... tan eficaz. -- -- 123456789 = 1.23456789 * 10^8 = {1, 8, {1,2,3,4,5,6,7,8,9}} -- log10(123456789) = 8 + log10(1.23456789) = 8 + 1 + log10(0.123456789) -- export function ba_log10(object x) if not bigatom(x) then x = ba_new(x) end if integer exponent = x[EXPONENT] + 1 x[EXPONENT] = -1 sequence res = ba_logb(x, 10) return ba_add(res, exponent) end function --
2. Re: bigatom.e - New ba_log10() function (fast)
- Posted by _tom (admin) Dec 23, 2014
- 1148 views
With shortcut to large numbers
Log10 (x * y) = log10 (x) + log10 (y)
And if 'x' or 'y' is a power of ten, the logarithm is the Exponent and simply add it to the logarithm of another number with exponent 0 So not a big number and calculates faster ... - ... The logarithm of a small number and a sum - Surprises me not having fallen before ... so simple ... so effective. - -
123456789 = 1.23456789 * 10 ^ 8 = {1, 8, {1,2,3,4,5,6,7,8,9}} Log10 (123456789) = 8 + log10 (1.23456789) = 8 + 1 + log10 (0.123456789)
3. Re: bigatom.e - New ba_log10() function (fast)
- Posted by cargoan Dec 24, 2014
- 1123 views
- Last edited Dec 25, 2014
ba_log10() sometimes, many times, returns more decimals than the scale if mode (SC_MODE) is not zero.
bigatom not counts leading zeros in small numbers (0.0...ddd...) and sum adds it when the res parameter is 0.0...ddd...
two possibilities:
-- fast export function ba_log10(object x, integer round = 0) if not bigatom(x) then x = ba_new(x) end if integer exponent = x[EXPONENT] + 1 x[EXPONENT] = -1 sequence res = ba_logb(x, 10, round) res = ba_add(res, exponent) -- la suma ignora la escala if round then res = round_digits(res, res[EXPONENT] + 2 + SCALE) else res[DIGITS] = remove(res[DIGITS], res[EXPONENT] + 2 + SCALE, length(res[DIGITS])) end if return normalize(res) end function -- -- KISS: Keep It Simple, Stupid!... ?????? -- KISS: Keep It Simple, Short... mejor así... mucho mejor. export function ba_log10_2(object x, integer round = 0) if not bigatom(x) then x = ba_new(x) end if integer exponent = x[EXPONENT] x[EXPONENT] = 0 sequence res = ba_logb(x, 10, round) return ba_add(res, exponent) end function --
but:
┌───────────────────────────────────────────────────[miércoles, 24 de diciembre 8:33 CET] ├─[cargoan en ~cargoan/Documentos/Programación/Euphoria/bigatom-a1] └─[bash 4.3]──[$]: tail -n 12 bigatom.e ifdef TEST2 then scale(100, 1) atom t0 = time() -- exp = -1 and cut ba_printf(1,"ba_log10('9999999999999999'):\n\t%B\n", ba_log10("9999999999999999") ) atom t1 = time() printf(1, "en %.3f seg.\n\n", t1 - t0) -- exp = 0 ba_printf(1,"ba_log10_2('9999999999999999'):\n\t%B\n", ba_log10_2("999999999999999 9")) printf(1, "en %.3f seg.\n", time() - t1) end ifdef ┌───────────────────────────────────────────────────[miércoles, 24 de diciembre 8:33 CET] ├─[cargoan en ~cargoan/Documentos/Programación/Euphoria/bigatom-a1] └─[bash 4.3]──[$]: eui -D TEST2 bigatom.e ba_log10('9999999999999999'): 15.99999999999999995657055180967481506341469859208020875008840375265452417821570583349 34058543959866208 en 0.820 seg. ba_log10_2('9999999999999999'): 15.99999999999999995657055180967481506341469859208020875008840375265452417821570583349 34058543959866208 en 1.500 seg. ┌───────────────────────────────────────────────────[miércoles, 24 de diciembre 8:33 CET] ├─[cargoan en ~cargoan/Documentos/Programación/Euphoria/bigatom-a1] └─[bash 4.3]──[$]: euc -D TEST2 bigatom.e Build directory: build-031070/ Translating code, pass: 1 2 3 4 5 6 7 8 generating Compiling with GCC Compiling 12% init-.c Compiling 50% bigatom.c Compiling 75% main-.c Linking 100% ../bigatom ┌───────────────────────────────────────────────────[miércoles, 24 de diciembre 8:33 CET] ├─[cargoan en ~cargoan/Documentos/Programación/Euphoria/bigatom-a1] └─[bash 4.3]──[$]: ./bigatom ba_log10('9999999999999999'): 15.99999999999999995657055180967481506341469859208020875008840375265452417821570583349 34058543959866208 en 0.250 seg. ba_log10_2('9999999999999999'): 15.99999999999999995657055180967481506341469859208020875008840375265452417821570583349 34058543959866208 en 0.410 seg. ┌───────────────────────────────────────────────────[miércoles, 24 de diciembre 8:33 CET] ├─[cargoan en ~cargoan/Documentos/Programación/Euphoria/bigatom-a1] └─[bash 4.3]──[$]: grep "model name" /proc/cpuinfo | sort -u | sed 's/.*: //' ; cat /sys/ devices/system/cpu/cpu0/cpufreq/{scaling_governor,cpuinfo_max_freq}; uname -a AMD Athlon(tm) Dual Core Processor 4450e ondemand 2300000 Linux HP-s3733es 3.17.7-1-lqx #1 ZEN SMP PREEMPT Fri Dec 19 15:52:45 CET 2014 x86_64 GNU/L inux ┌───────────────────────────────────────────────────[miércoles, 24 de diciembre 8:33 CET] ├─[cargoan en ~cargoan/Documentos/Programación/Euphoria/bigatom-a1] └─[bash 4.3]──[$]: journalctl -b | grep -i smpboot | grep -i mips | sed 's|.*:*: ||' Total of 2 processors activated (9200.10 BogoMIPS) ┌───────────────────────────────────────────────────[miércoles, 24 de diciembre 8:33 CET] ├─[cargoan en ~cargoan/Documentos/Programación/Euphoria/bigatom-a1] └─[bash 4.3]──[$]: gcc --version | head -n 2 gcc (GCC) 4.9.2 Copyright (C) 2014 Free Software Foundation, Inc.