Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of SQRT #14

Closed
zikajk opened this issue Mar 7, 2024 · 2 comments
Closed

Implementation of SQRT #14

zikajk opened this issue Mar 7, 2024 · 2 comments
Assignees

Comments

@zikajk
Copy link

zikajk commented Mar 7, 2024

Hi, I had to implement some functions, but unfortunately SQRT relies on the internal API.
What do you think about making them part of broch.core?

(defn pow [a b]
  (reduce b/* (repeat b a)))

(defn derive-sqrt-comp [x]
  (->> x
       b/composition
       (map (fn [[k v]] (if (= :broch/scaled k) 
                                     [k #?(:clj (rationalize (math/sqrt v))
                                         :cljs (math/sqrt v))] 
                                     [k (/ v 2)])))
       (into {})))
       
(defn sqrt [x]
  (let [result (math/sqrt (b/num x))
        composition (derive-sqrt-comp x)
        new-unit (get @broch.impl/composition-registry composition)]
    (when new-unit
      (broch.impl/quantity new-unit result))))
@zikajk zikajk changed the title Implementation of SQRT and POW Implementation of SQRT Mar 7, 2024
@2food 2food self-assigned this Apr 3, 2024
@2food
Copy link
Contributor

2food commented Apr 3, 2024

Hi @zikajk.

We could add pow and sqrt to broch.core but as of now I'm not sure I see the need to. It would additionally require implementing sqrt for a JSRatio, and probably some more. I consider the arithmetic api of +,-,*, and / as "complete", since that's what most users will ever really need for calculations with units. Do have a specific use case where you needed this?

@zikajk
Copy link
Author

zikajk commented Apr 4, 2024

Hi @2food,
My use is to support expressions like this:
10mm + (3m * pi()) ^ 2

You are probably right. I am still on "2023.12.15" where JSRatio doesn't exist yet.

@2food 2food closed this as completed Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants