unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* 1.75 -> 1-3/4
@ 2018-06-05 13:01 Emanuel Berg
  2018-06-05 13:18 ` Teemu Likonen
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: Emanuel Berg @ 2018-06-05 13:01 UTC (permalink / raw)
  To: help-gnu-emacs

Did anyone do the equivalent of this ("ths" zsh
below) in Elisp or any other Lisp? Just asking
before I do it myself. The algorithm should be
straightforward, but it uses recursion which
I plan to replace by a loop in the Elisp
version. But it looks good otherwise, right?

Before anyone tries to use it on a bicycle
tire, note that "[a] 26x1.5 tire and a 26x1-1/2
tire are different tire sizing systems" (JL on
rec.bicycles.tech). Read more here [1]

ths () {
    local value=$1
    local denom=${2:-16}
    local whole=$(( int(floor($value)) ))
    local rest=$(( $value - $whole ))
    local frac=$(( int(rint($rest * $denom)) ))
    if (( $frac > 0 && $(( $frac % 2 )) == 0 )); then
        local new_denom=$(( denom / 2 ))
        ths $value $new_denom
    else
        local frace
        (( $frac > 0 )) && frace=-${frac}/${denom}
        echo ${whole}${frace}
    fi
}
# $ ths 2.0
# 2
# $ ths 1.75
# 1-3/4

[1] http://www.sheldonbrown.com/tire-sizing.html

-- 
underground experts united
http://user.it.uu.se/~embe8573


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

* Re: 1.75 -> 1-3/4
  2018-06-05 13:01 1.75 -> 1-3/4 Emanuel Berg
@ 2018-06-05 13:18 ` Teemu Likonen
  2018-06-05 13:39   ` tomas
       [not found]   ` <mailman.1205.1528205966.1292.help-gnu-emacs@gnu.org>
  2018-06-05 15:25 ` Marko Vojinovic
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 11+ messages in thread
From: Teemu Likonen @ 2018-06-05 13:18 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 662 bytes --]

Emanuel Berg [2018-06-05 15:01:33+02] wrote:

> Did anyone do the equivalent of this ("ths" zsh below) in Elisp or any
> other Lisp?

Common Lisp has RATIONAL and RATIONALIZE functions which convert real
numbers to rational numbers. See the specification:

    http://www.lispworks.com/documentation/HyperSpec/Body/f_ration.htm

"Decimals" library for Common Lisp can parse a decimal number string and
convert it to a rational number. See function PARSE-DECIMAL-NUMBER:

    https://github.com/tlikonen/cl-decimals

-- 
/// Teemu Likonen   - .-..   <https://keybase.io/tlikonen> //
// PGP: 4E10 55DC 84E9 DFF6 13D7 8557 719D 69D3 2453 9450 ///

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]

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

* Re: 1.75 -> 1-3/4
  2018-06-05 13:18 ` Teemu Likonen
@ 2018-06-05 13:39   ` tomas
  2018-06-05 13:49     ` tomas
       [not found]   ` <mailman.1205.1528205966.1292.help-gnu-emacs@gnu.org>
  1 sibling, 1 reply; 11+ messages in thread
From: tomas @ 2018-06-05 13:39 UTC (permalink / raw)
  To: help-gnu-emacs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, Jun 05, 2018 at 04:18:23PM +0300, Teemu Likonen wrote:
> Emanuel Berg [2018-06-05 15:01:33+02] wrote:
> 
> > Did anyone do the equivalent of this ("ths" zsh below) in Elisp or any
> > other Lisp?
> 
> Common Lisp has RATIONAL and RATIONALIZE functions which convert real
> numbers to rational numbers. See the specification:
> 
>     http://www.lispworks.com/documentation/HyperSpec/Body/f_ration.htm
> 
> "Decimals" library for Common Lisp can parse a decimal number string and
> convert it to a rational number. See function PARSE-DECIMAL-NUMBER:
> 
>     https://github.com/tlikonen/cl-decimals

GNU Guile can do this too:

  scheme@(guile-user)> (inexact->exact 1.3333)
  $7 = 3002324691586541/2251799813685248

(yikes ;-) but...

  scheme@(guile-user)> (rationalize (inexact->exact 1.3333) 1/1000)
  $10 = 4/3

(ah, better). Note that the second argument to rationalize *has* to
be a fraction, otherwise the result will be inexact (i.e. a float).

As to Emacs lisp... natively not. But there seems to be a package [1],
and good ol' calc can deal with rationals too.

Now if we had Guile Emacs (I know, I know).

Cheers

[1] https://www.emacswiki.org/emacs/RationalNumber
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlsWkn4ACgkQBcgs9XrR2kYFcACfd5vldWofHzr3AvmZbeQqE+9r
1E0An0xTK5IRXElTyHWNwWQ4mzsQfRG2
=mbKJ
-----END PGP SIGNATURE-----



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

* Re: 1.75 -> 1-3/4
  2018-06-05 13:39   ` tomas
@ 2018-06-05 13:49     ` tomas
  2018-06-05 14:12       ` Teemu Likonen
  0 siblings, 1 reply; 11+ messages in thread
From: tomas @ 2018-06-05 13:49 UTC (permalink / raw)
  To: help-gnu-emacs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, Jun 05, 2018 at 03:39:10PM +0200, tomas@tuxteam.de wrote:
> On Tue, Jun 05, 2018 at 04:18:23PM +0300, Teemu Likonen wrote:
> > Emanuel Berg [2018-06-05 15:01:33+02] wrote:
> > 
> > > Did anyone do the equivalent of this ("ths" zsh below) in Elisp or any
> > > other Lisp?
> > 
> > Common Lisp has RATIONAL and RATIONALIZE functions which convert real
> > numbers to rational numbers. See the specification:

Uh... just to be sure, Teemu: you were talking about CL "The Real Thing",
not about Emacs' cl, right?

Cheers
- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlsWlOMACgkQBcgs9XrR2kb/iQCePITjHy0VY1TmHv1J27+lZx0G
0EUAnj1T6rrvwXs1zQddIgXU93Aco6tu
=eNi9
-----END PGP SIGNATURE-----



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

* Re: 1.75 -> 1-3/4
  2018-06-05 13:49     ` tomas
@ 2018-06-05 14:12       ` Teemu Likonen
  0 siblings, 0 replies; 11+ messages in thread
From: Teemu Likonen @ 2018-06-05 14:12 UTC (permalink / raw)
  To: tomas; +Cc: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 590 bytes --]

tomas@tuxteam.de [2018-06-05 15:49:23+02] wrote:

> On Tue, Jun 05, 2018 at 03:39:10PM +0200, tomas@tuxteam.de wrote:
>>> Common Lisp has RATIONAL and RATIONALIZE functions which convert
>>> real numbers to rational numbers. See the specification:
>
> Uh... just to be sure, Teemu: you were talking about CL "The Real
> Thing", not about Emacs' cl, right?

I talked about the real Common Lisp programming language which has
rational number type.

-- 
/// Teemu Likonen   - .-..   <https://keybase.io/tlikonen> //
// PGP: 4E10 55DC 84E9 DFF6 13D7 8557 719D 69D3 2453 9450 ///

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 487 bytes --]

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

* Re: 1.75 -> 1-3/4
  2018-06-05 13:01 1.75 -> 1-3/4 Emanuel Berg
  2018-06-05 13:18 ` Teemu Likonen
@ 2018-06-05 15:25 ` Marko Vojinovic
       [not found] ` <mailman.1238.1528215089.1292.help-gnu-emacs@gnu.org>
       [not found] ` <mailman.1199.1528204714.1292.help-gnu-emacs@gnu.org>
  3 siblings, 0 replies; 11+ messages in thread
From: Marko Vojinovic @ 2018-06-05 15:25 UTC (permalink / raw)
  To: help-gnu-emacs

On Tue, 05 Jun 2018 15:01:33 +0200
Emanuel Berg <moasen@zoho.com> wrote:

> ths () {
>     local value=$1
>     local denom=${2:-16}
>     local whole=$(( int(floor($value)) ))
>     local rest=$(( $value - $whole ))
>     local frac=$(( int(rint($rest * $denom)) ))
>     if (( $frac > 0 && $(( $frac % 2 )) == 0 )); then
>         local new_denom=$(( denom / 2 ))
>         ths $value $new_denom
>     else
>         local frace
>         (( $frac > 0 )) && frace=-${frac}/${denom}

Umm, not to be a nitpick, but how about you change the minus in the
above line into a plus?

I mean, 1.75 is equal to 1+3/4, rather than 1-3/4 (the latter
obviously being equal to 0.25). I kind-of assume you know what you are
doing, but still, other people may get confused.

>         echo ${whole}${frace}
>     fi
> }
> # $ ths 2.0
> # 2
> # $ ths 1.75
> # 1-3/4
> 

HTH, :-)
Marko





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

* Re: 1.75 -> 1-3/4
       [not found]   ` <mailman.1205.1528205966.1292.help-gnu-emacs@gnu.org>
@ 2018-06-05 15:30     ` Emanuel Berg
  2018-06-05 17:00       ` tomas
       [not found]       ` <mailman.1245.1528218012.1292.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 11+ messages in thread
From: Emanuel Berg @ 2018-06-05 15:30 UTC (permalink / raw)
  To: help-gnu-emacs

tomas wrote:

>   scheme@(guile-user)> (inexact->exact 1.3333)
>   $7 = 3002324691586541/2251799813685248
>
> (yikes ;-) but...

Ha ha :)

> [1]
> https://www.emacswiki.org/emacs/RationalNumber

Did you try it and get it to work for 1.75 -> 1-3/4?

-- 
underground experts united
http://user.it.uu.se/~embe8573


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

* Re: 1.75 -> 1-3/4
  2018-06-05 15:30     ` Emanuel Berg
@ 2018-06-05 17:00       ` tomas
       [not found]       ` <mailman.1245.1528218012.1292.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 11+ messages in thread
From: tomas @ 2018-06-05 17:00 UTC (permalink / raw)
  To: help-gnu-emacs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tue, Jun 05, 2018 at 05:30:52PM +0200, Emanuel Berg wrote:
> tomas wrote:
> 
> >   scheme@(guile-user)> (inexact->exact 1.3333)
> >   $7 = 3002324691586541/2251799813685248
> >
> > (yikes ;-) but...
> 
> Ha ha :)
> 
> > [1]
> > https://www.emacswiki.org/emacs/RationalNumber
> 
> Did you try it and get it to work for 1.75 -> 1-3/4?

Well, as posted I'd expect 7/4, let's see...

  scheme@(guile-user)> (rationalize (inexact->exact 1.75) 1/1000)
  $1 = 7/4

So one'd need to muck around with floor and things...

Cheers
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlsWwZEACgkQBcgs9XrR2kZlNgCdHq55CXYx5hg2CWsNcVcKQ1uD
KgwAnRCvmdVs8jxmD/8nbnoZhShNljUt
=EDkQ
-----END PGP SIGNATURE-----



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

* Re: 1.75 -> 1-3/4
       [not found] ` <mailman.1238.1528215089.1292.help-gnu-emacs@gnu.org>
@ 2018-06-05 18:06   ` Emanuel Berg
  0 siblings, 0 replies; 11+ messages in thread
From: Emanuel Berg @ 2018-06-05 18:06 UTC (permalink / raw)
  To: help-gnu-emacs

Marko Vojinovic wrote:

>> ths () {
>>     local value=$1
>>     local denom=${2:-16}
>>     local whole=$(( int(floor($value)) ))
>>     local rest=$(( $value - $whole ))
>>     local frac=$(( int(rint($rest * $denom)) ))
>>     if (( $frac > 0 && $(( $frac % 2 )) == 0 )); then
>>         local new_denom=$(( denom / 2 ))
>>         ths $value $new_denom
>>     else
>>         local frace
>>         (( $frac > 0 )) && frace=-${frac}/${denom}
>
> Umm, not to be a nitpick, but how about you
> change the minus in the above line into
> a plus?
>
> I mean, 1.75 is equal to 1+3/4, rather than
> 1-3/4 (the latter obviously being equal to
> 0.25). I kind-of assume you know what you are
> doing, but still, other people may
> get confused.

Ha ha :D

-- 
underground experts united
http://user.it.uu.se/~embe8573


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

* Re: 1.75 -> 1-3/4
       [not found]       ` <mailman.1245.1528218012.1292.help-gnu-emacs@gnu.org>
@ 2018-06-05 18:08         ` Emanuel Berg
  0 siblings, 0 replies; 11+ messages in thread
From: Emanuel Berg @ 2018-06-05 18:08 UTC (permalink / raw)
  To: help-gnu-emacs

tomas wrote:

>>> [1] https://www.emacswiki.org/emacs/RationalNumber
>>  Did you try it and get it to work for 1.75
>> -> 1-3/4?
>
> Well, as posted I'd expect 7/4, let's see...
>
>   scheme@(guile-user)> (rationalize
> (inexact->exact 1.75) 1/1000) $1 = 7/4
>
> So one'd need to muck around with floor and
> things...

Heh, I meant the rational.el in the URL
you posted!

-- 
underground experts united
http://user.it.uu.se/~embe8573


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

* Re: 1.75 -> 1-3/4
       [not found] ` <mailman.1199.1528204714.1292.help-gnu-emacs@gnu.org>
@ 2018-06-05 18:54   ` Emanuel Berg
  0 siblings, 0 replies; 11+ messages in thread
From: Emanuel Berg @ 2018-06-05 18:54 UTC (permalink / raw)
  To: help-gnu-emacs

Teemu Likonen wrote:

> "Decimals" library for Common Lisp can parse
> a decimal number string and
> convert it to a rational number. See function PARSE-DECIMAL-NUMBER:
>
>     https://github.com/tlikonen/cl-decimals

Good work!

-- 
underground experts united
http://user.it.uu.se/~embe8573


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

end of thread, other threads:[~2018-06-05 18:54 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-06-05 13:01 1.75 -> 1-3/4 Emanuel Berg
2018-06-05 13:18 ` Teemu Likonen
2018-06-05 13:39   ` tomas
2018-06-05 13:49     ` tomas
2018-06-05 14:12       ` Teemu Likonen
     [not found]   ` <mailman.1205.1528205966.1292.help-gnu-emacs@gnu.org>
2018-06-05 15:30     ` Emanuel Berg
2018-06-05 17:00       ` tomas
     [not found]       ` <mailman.1245.1528218012.1292.help-gnu-emacs@gnu.org>
2018-06-05 18:08         ` Emanuel Berg
2018-06-05 15:25 ` Marko Vojinovic
     [not found] ` <mailman.1238.1528215089.1292.help-gnu-emacs@gnu.org>
2018-06-05 18:06   ` Emanuel Berg
     [not found] ` <mailman.1199.1528204714.1292.help-gnu-emacs@gnu.org>
2018-06-05 18:54   ` Emanuel Berg

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).