unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Floating Point computations in elisp
@ 2020-12-18  8:17 Christopher Dimech
  2020-12-18  8:22 ` Michael Heerdegen
  2020-12-18 17:08 ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 2 replies; 12+ messages in thread
From: Christopher Dimech @ 2020-12-18  8:17 UTC (permalink / raw)
  To: Help Gnu Emacs


How can I do floating point computations in elisp

(setq frc (/ tpd 60.0))



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Floating Point computations in elisp
  2020-12-18  8:17 Floating Point computations in elisp Christopher Dimech
@ 2020-12-18  8:22 ` Michael Heerdegen
  2020-12-18  8:34   ` Christopher Dimech
  2020-12-18 11:07   ` Jean Louis
  2020-12-18 17:08 ` Emanuel Berg via Users list for the GNU Emacs text editor
  1 sibling, 2 replies; 12+ messages in thread
From: Michael Heerdegen @ 2020-12-18  8:22 UTC (permalink / raw)
  To: help-gnu-emacs

Christopher Dimech <dimech@gmx.com> writes:

> How can I do floating point computations in elisp
>
> (setq frc (/ tpd 60.0))

Like (/ (float tpd) 60.0).

Michael.




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Floating Point computations in elisp
  2020-12-18  8:22 ` Michael Heerdegen
@ 2020-12-18  8:34   ` Christopher Dimech
  2020-12-18 11:07   ` Jean Louis
  1 sibling, 0 replies; 12+ messages in thread
From: Christopher Dimech @ 2020-12-18  8:34 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs

Good morning and many thanks Michael.

---------------------
Christopher Dimech
General Administrator - Naiad Informatics - GNU Project (Geocomputation)
- Geophysical Simulation
- Geological Subsurface Mapping
- Disaster Preparedness and Mitigation
- Natural Resource Exploration and Production
- Free Software Advocacy


> Sent: Friday, December 18, 2020 at 9:22 AM
> From: "Michael Heerdegen" <michael_heerdegen@web.de>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Floating Point computations in elisp
>
> Christopher Dimech <dimech@gmx.com> writes:
>
> > How can I do floating point computations in elisp
> >
> > (setq frc (/ tpd 60.0))
>
> Like (/ (float tpd) 60.0).
>
> Michael.
>
>
>



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Floating Point computations in elisp
  2020-12-18  8:22 ` Michael Heerdegen
  2020-12-18  8:34   ` Christopher Dimech
@ 2020-12-18 11:07   ` Jean Louis
  1 sibling, 0 replies; 12+ messages in thread
From: Jean Louis @ 2020-12-18 11:07 UTC (permalink / raw)
  To: Michael Heerdegen; +Cc: help-gnu-emacs

* Michael Heerdegen <michael_heerdegen@web.de> [2020-12-18 11:23]:
> Christopher Dimech <dimech@gmx.com> writes:
> 
> > How can I do floating point computations in elisp
> >
> > (setq frc (/ tpd 60.0))
> 
> Like (/ (float tpd) 60.0).

Thank you. But in the above example there is decimal 60.0 that would
do the same result.

(eql (/ (float 1) 60.0) (/ 1 60.0))
(eql (/ (float 20) 60.0) (/ 20 60.0))

While this would not be same:
(eql (/ 20 (float 60)) (/ 20 60))

because I did not know `float' I was always adding or using something
like 1.0 in divisions, like:

(/ 10 30 1.0) to get the same effect.

Now when I know `float' I could use:

(/ (float 10) 30)

As it is more descriptive, it gives maybe more explanation in the code
on WHY, as inserting 1.0 there does not necessarily makes it clear
WHY.






^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Floating Point computations in elisp
  2020-12-18  8:17 Floating Point computations in elisp Christopher Dimech
  2020-12-18  8:22 ` Michael Heerdegen
@ 2020-12-18 17:08 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-12-18 18:21   ` Jean Louis
  1 sibling, 1 reply; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-18 17:08 UTC (permalink / raw)
  To: help-gnu-emacs

> How can I do floating point computations in elisp
>
> (setq frc (/ tpd 60.0))

As you did, just add a .0 to avoid integer division.

Other than that, just compute whatever you like.

(defun mean-value (vs)
  (let*((sum  (apply #'+ vs))
        (mean (/ sum (length vs) 1.0)) )
    mean) )

(defun hypotenuse (c1 c2)
  (sqrt (+ (* c1 c1) (* c2 c2))) )

(defun speed (hour minute second km)
  (let*((s (+ second (* 60 minute) (* 60 60 hour)))
        (m (* 1000 km))
        (mps (/ m s)) )
    (* 3.6 mps) ))

Hm ... what does that do?

Anyway, you can then use `format' to - that's right - format
the output. It is very versatile, see the docstring.

(defun compute-space-distance (dist num-beams beam-width)
  (let*((spaces         (1+ num-beams))
        (dist-covered   (* num-beams beam-width))
        (dist-uncovered (- dist dist-covered))
        (space          (/ dist-uncovered spaces 1.0) ))
    space ))

;; (insert (format "\n;; %.2f" (compute-space-distance 262 16 12)))
;; 4.12

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Floating Point computations in elisp
  2020-12-18 17:08 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-12-18 18:21   ` Jean Louis
  2020-12-20  0:38     ` Emanuel Berg via Users list for the GNU Emacs text editor
                       ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Jean Louis @ 2020-12-18 18:21 UTC (permalink / raw)
  To: help-gnu-emacs

* Emanuel Berg via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org> [2020-12-18 20:09]:
> (defun compute-space-distance (dist num-beams beam-width)
>   (let*((spaces         (1+ num-beams))
>         (dist-covered   (* num-beams beam-width))
>         (dist-uncovered (- dist dist-covered))
>         (space          (/ dist-uncovered spaces 1.0) ))
>     space ))
> 
> ;; (insert (format "\n;; %.2f" (compute-space-distance 262 16 12)))
> ;; 4.12

If I wish to arrive to Tau Ceti from Earth and it is maybe 11.8 light
years distant, how many years do I need to get there provided I trave
5,000 km per hour?




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Floating Point computations in elisp
  2020-12-18 18:21   ` Jean Louis
@ 2020-12-20  0:38     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-12-20  0:42     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-12-20  0:45     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2 siblings, 0 replies; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-20  0:38 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

> If I wish to arrive to Tau Ceti from Earth and it is maybe
> 11.8 light years distant, how many years do I need to get
> there provided I trave 5,000 km per hour?

I don't know, 215 981 years?

(defun tau-ceti (speed)
  (let*((dist  (* 9.46 (expt 10 12)))
        (time  (/ dist speed) )
        (hours (round time)) )
    (format-seconds "%y years"
                    (- (float-time (encode-time 0 0 hours 0 0 0))
                       (float-time (encode-time 0 0     0 0 0 0)) ))))
;; (tau-ceti 5000)

--
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Floating Point computations in elisp
  2020-12-18 18:21   ` Jean Louis
  2020-12-20  0:38     ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-12-20  0:42     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-12-20  0:45     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2 siblings, 0 replies; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-20  0:42 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

> If I wish to arrive to Tau Ceti from Earth and it is maybe
> 11.8 light years distant, how many years do I need to get
> there provided I trave 5,000 km per hour?

Oups, forgot the 11.8 part in the post just sent.

But then, "float-time: Specified time is not representable".

So must convert the hours into days first, perhaps...

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Floating Point computations in elisp
  2020-12-18 18:21   ` Jean Louis
  2020-12-20  0:38     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-12-20  0:42     ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-12-20  0:45     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-12-20  3:43       ` Christopher Dimech
  2 siblings, 1 reply; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-12-20  0:45 UTC (permalink / raw)
  To: help-gnu-emacs

Jean Louis wrote:

> * Emanuel Berg via Users list for the GNU Emacs text editor
> <help-gnu-emacs@gnu.org> [2020-12-18 20:09]:
>
>> (defun compute-space-distance (dist num-beams beam-width)
>>   (let*((spaces         (1+ num-beams))
>>         (dist-covered   (* num-beams beam-width))
>>         (dist-uncovered (- dist dist-covered))
>>         (space          (/ dist-uncovered spaces 1.0) ))
>>     space ))
>> 
>> ;; (insert (format "\n;; %.2f" (compute-space-distance 262 16 12)))
>> ;; 4.12
>
> If I wish to arrive to Tau Ceti from Earth and it is maybe 11.8 light
> years distant, how many years do I need to get there provided I trave
> 5,000 km per hour?

How about: 2 548 584 years

(defun tau-ceti (speed)
  (let*((dist       (* 11.8 9.46 (expt 10 12)))
        (time       (/ dist speed) )
        (days       (/ time 24))
        (whole-days (round days)) )
    (format-seconds "%y years"
                    (- (float-time (encode-time 0 0 0 whole-days 0 0))
                       (float-time (encode-time 0 0 0 0 0 0)) ))))
;; (tau-ceti 5000)

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Floating Point computations in elisp
  2020-12-20  0:45     ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-12-20  3:43       ` Christopher Dimech
  2020-12-20  4:14         ` Stefan Monnier
  0 siblings, 1 reply; 12+ messages in thread
From: Christopher Dimech @ 2020-12-20  3:43 UTC (permalink / raw)
  To: moasenwood; +Cc: help-gnu-emacs



> Sent: Sunday, December 20, 2020 at 6:15 AM
> From: "Emanuel Berg via Users list for the GNU Emacs text editor" <help-gnu-emacs@gnu.org>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Floating Point computations in elisp
>
> Jean Louis wrote:
>
> > * Emanuel Berg via Users list for the GNU Emacs text editor
> > <help-gnu-emacs@gnu.org> [2020-12-18 20:09]:
> >
> >> (defun compute-space-distance (dist num-beams beam-width)
> >>   (let*((spaces         (1+ num-beams))
> >>         (dist-covered   (* num-beams beam-width))
> >>         (dist-uncovered (- dist dist-covered))
> >>         (space          (/ dist-uncovered spaces 1.0) ))
> >>     space ))
> >>
> >> ;; (insert (format "\n;; %.2f" (compute-space-distance 262 16 12)))
> >> ;; 4.12
> >
> > If I wish to arrive to Tau Ceti from Earth and it is maybe 11.8 light
> > years distant, how many years do I need to get there provided I trave
> > 5,000 km per hour?
>
> How about: 2 548 584 years

The ultimate question is then, how much will it cost.  :)

> (defun tau-ceti (speed)
>   (let*((dist       (* 11.8 9.46 (expt 10 12)))
>         (time       (/ dist speed) )
>         (days       (/ time 24))
>         (whole-days (round days)) )
>     (format-seconds "%y years"
>                     (- (float-time (encode-time 0 0 0 whole-days 0 0))
>                        (float-time (encode-time 0 0 0 0 0 0)) ))))
> ;; (tau-ceti 5000)
>
> --
> underground experts united
> http://user.it.uu.se/~embe8573
> https://dataswamp.org/~incal
>
>
>



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Floating Point computations in elisp
  2020-12-20  3:43       ` Christopher Dimech
@ 2020-12-20  4:14         ` Stefan Monnier
  2020-12-20  4:19           ` Christopher Dimech
  0 siblings, 1 reply; 12+ messages in thread
From: Stefan Monnier @ 2020-12-20  4:14 UTC (permalink / raw)
  To: help-gnu-emacs

>> How about: 2 548 584 years
>
> The ultimate question is then, how much will it cost.  :)

Expresssed in dollars of when you leave or dollars of when you arrive?


        Stefan




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Floating Point computations in elisp
  2020-12-20  4:14         ` Stefan Monnier
@ 2020-12-20  4:19           ` Christopher Dimech
  0 siblings, 0 replies; 12+ messages in thread
From: Christopher Dimech @ 2020-12-20  4:19 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs



> Sent: Sunday, December 20, 2020 at 9:44 AM
> From: "Stefan Monnier" <monnier@iro.umontreal.ca>
> To: help-gnu-emacs@gnu.org
> Subject: Re: Floating Point computations in elisp
>
> >> How about: 2 548 584 years
> >
> > The ultimate question is then, how much will it cost.  :)
>
> Expresssed in dollars of when you leave or dollars of when you arrive?

Inclusive of the return trip.  Customarily  you pay before you leave.

>         Stefan
>
>
>



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2020-12-20  4:19 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-12-18  8:17 Floating Point computations in elisp Christopher Dimech
2020-12-18  8:22 ` Michael Heerdegen
2020-12-18  8:34   ` Christopher Dimech
2020-12-18 11:07   ` Jean Louis
2020-12-18 17:08 ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-18 18:21   ` Jean Louis
2020-12-20  0:38     ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-20  0:42     ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-20  0:45     ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-12-20  3:43       ` Christopher Dimech
2020-12-20  4:14         ` Stefan Monnier
2020-12-20  4:19           ` Christopher Dimech

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).