Comments (9)
Better version based on current features:
(defun-bind str-to-num (_)
;(str-to-num str) -> num
(defq n 0 b 10 f 0)
(when (> (length _) 1)
(defq i (elem 1 _))
(cond
((eql i "x")
(setq b 16 _ (slice 2 -1 _)))
((eql i "o")
(setq b 8 _ (slice 2 -1 _)))
((eql i "b")
(setq b 2 _ (slice 2 -1 _)))))
(each (# (if (eql %0 ".")
(setq f 1)
(setq n (+ (* n b) (char-to-num %0)) f (* f b)))) _)
(if (= f 0) n (/ (<< n fp_shift) f)))
I'll push this as it is faster ! Note the cute use of Franks (#) function shortcuts now :) That was such a good addition. :)
from chrysalisp.
This is producing a fixed point number as an int ! Yes I know this is a little 'unfinished' I been adding the polymophic number stuff to replace all this for a while, not covered all cases yet. !
from chrysalisp.
You can even do 0x45.67 ! Hex base fixed point...
from chrysalisp.
-0 is just plain wrong. I just didn't care about that case at the time.
from chrysalisp.
May also find it doesn’t do negative numbers at all !
That’s just because where it first got used was for a Lisp version of the built in read_num when I did the C-Script parser and it never called it needing to handle negativity.
But this is probably better done these days as a binding to the internal read_num routines. Which I can look at next if you want.
from chrysalisp.
That would be good as a general thing. My immediate usage is to have type checking conversion of a command line arg to an integer or float (double? real?).
from chrysalisp.
Both are really hacky, but I have s2i for integers in the date lib. It handles negative integers, including -0, but it will only work on strings up to length 12 or so. I assume it's some n-bit integer constraint I hit while adding values.
I wrote s2n to handle floats, it uses s2i and i2f for all the heavy lifting but since division of 1.0 by 10.0 results in 0.09999 it doesn't quite work as desired. It only works properly if there is nothing but a 0 after the decimal.
(defun-bind s2i (s)
(let ((final 0) (index 1) (iszero nil) (isnum t) (isneg nil))
(cond
((and (= (length s) 1) (eql (code s) 48)) (setq iszero t))
((starts-with "-" s) (setq isneg t s (slice 1 -1 s)))
(t nil))
(if (some (lambda (c) (not (< 47 (code c) 58))) s) (setq final nil))
(if (and final (not iszero)) (each (lambda (c)
(setq final (+ final (* (- (code c) 48) index))
index (* index 10))) (reverse s)))
(if isneg (setq final (- 0 final)))
final))
(defun-bind s2n (s)
(defq divisor 1)
(cond
((find "." s)
(+ (i2f (s2i (first (split s "."))))
(/ (i2f (s2i (defq decimal (second (split s ".")))))
(i2f (each (lambda (_) (setq divisor (* 10 divisor)))
(range 0 (length decimal)))))))
(t (s2i s))))
from chrysalisp.
Current (str-to-num)
hack in boot.inc:
(defun-bind str-to-num (_)
;(str-to-num str) -> num
(defq n 0 b 10)
(when (> (length _) 1)
(defq i (elem 1 _))
(cond
((eql i "x")
(setq b 16 _ (slice 2 -1 _)))
((eql i "o")
(setq b 8 _ (slice 2 -1 _)))
((eql i "b")
(setq b 2 _ (slice 2 -1 _)))))
(defq i -1 f 0)
(while (< (setq i (inc i)) (length _))
(defq c (elem i _))
(if (eql c ".")
(setq f 1)
(defq d (char-to-num c) n (+ (* n b) d) f (* f b))))
(if (= f 0) n (/ (<< n fp_shift) f)))
This tries to guess the base of the number, then shifts in the digits in that base. If it see a '.' on route it remembers the position of that and turns it into a fixed point as the last thing.
Was very quick and dirty at the time of writing. Didn't test for leading -, didn't cope with errors of any kind etc
from chrysalisp.
pushed a version that copes with uppercase type chars.
from chrysalisp.
Related Issues (20)
- Class serialize/deserialize HOT 1
- Refactor IPC to modularize for extension and declare TERMINAL_SERVICE
- Update IPC to use new AST conversions as a form of message interchange
- DLS flag and Timezone from Host System.
- Update YAML data
- Fix Logging for new mail-box conventions HOT 1
- Transition Log Service to new IPC and data exchangers HOT 1
- Fix yaml string quote compounding
- Display scaling options? HOT 5
- src/main.cpp:37:11: fatal error: 'SDL.h' file not found HOT 5
- Package manager needed HOT 3
- @Chris HOT 16
- Containers woven in to foundations HOT 1
- Interprocess communication port needed HOT 4
- WYSIWYG interface builder HOT 6
- A browser for ChrysaLisp pages. HOT 10
- WebDriver compatible functional UI clicking test automation HOT 1
- reload definitions in lisp HOT 3
- error while compiling HOT 3
- error HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chrysalisp.