Git Product home page Git Product logo

Comments (9)

vygr avatar vygr commented on May 5, 2024 1

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.

vygr avatar vygr commented on May 5, 2024

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.

vygr avatar vygr commented on May 5, 2024

You can even do 0x45.67 ! Hex base fixed point...

from chrysalisp.

vygr avatar vygr commented on May 5, 2024

-0 is just plain wrong. I just didn't care about that case at the time.

from chrysalisp.

vygr avatar vygr commented on May 5, 2024

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.

FrankC01 avatar FrankC01 commented on May 5, 2024

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.

nuclearfall avatar nuclearfall commented on May 5, 2024

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.

vygr avatar vygr commented on May 5, 2024

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.

vygr avatar vygr commented on May 5, 2024

pushed a version that copes with uppercase type chars.

from chrysalisp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.