Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #38967 > unrolled thread
| Started by | beeflo <beeflobill@gmail.com> |
|---|---|
| First post | 2015-05-11 08:20 -0700 |
| Last post | 2015-05-11 16:55 +0000 |
| Articles | 12 — 7 participants |
Back to article view | Back to comp.lang.forth
How to convert whole numbers to floating? beeflo <beeflobill@gmail.com> - 2015-05-11 08:20 -0700
Re: How to convert whole numbers to floating? Paul Rubin <no.email@nospam.invalid> - 2015-05-11 08:28 -0700
Re: How to convert whole numbers to floating? beeflo <beeflobill@gmail.com> - 2015-05-11 08:57 -0700
Re: How to convert whole numbers to floating? krishna.myneni@ccreweb.org - 2015-05-12 16:36 -0700
Re: How to convert whole numbers to floating? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-05-13 16:04 +0000
Re: How to convert whole numbers to floating? krishna.myneni@ccreweb.org - 2015-05-14 18:41 -0700
Re: How to convert whole numbers to floating? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-05-15 07:23 +0000
Re: How to convert whole numbers to floating? Coos Haak <chforth@hccnet.nl> - 2015-05-11 17:43 +0200
Re: How to convert whole numbers to floating? beeflo <beeflobill@gmail.com> - 2015-05-11 08:58 -0700
Re: How to convert whole numbers to floating? rickman <gnuarm@gmail.com> - 2015-05-11 15:09 -0400
Re: How to convert whole numbers to floating? hughaguilar96@gmail.com - 2015-05-11 09:21 -0700
Re: How to convert whole numbers to floating? anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2015-05-11 16:55 +0000
| From | beeflo <beeflobill@gmail.com> |
|---|---|
| Date | 2015-05-11 08:20 -0700 |
| Subject | How to convert whole numbers to floating? |
| Message-ID | <765d92cc-590c-4da4-a0af-65f9116fbf52@googlegroups.com> |
I'm struggling to understand how conversion between the parameter stack and the floating stack works, using gforth. Right now, I'm just trying to convert whole numbers (from a loop index) to the floating stack. Through experimentation I've come up with a solution: : convert (u u -- ) F:( -- r ) dup 0 < if -1 else 0 then d>f ; However, I don't really understand it. Why do negative numbers require the -1 to be passed instead of 0? What does the top stack item mean to d>f? Is there a better way to do this? Where might I read to really get a good handle of floating point representation? Gracias.
[toc] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2015-05-11 08:28 -0700 |
| Message-ID | <87k2wf16cg.fsf@jester.gateway.sonic.net> |
| In reply to | #38967 |
beeflo <beeflobill@gmail.com> writes:
> I'm struggling to understand how conversion between the parameter
> stack and the floating stack works, using gforth.
> Right now, I'm just trying to convert whole numbers (from a loop
> index) to the floating stack.
I don't understand what you are trying to do. It sounds like you're
asking for s>f (single to float) since d>f converts a double width integer.
Example (gforth 0.7.0):
: test cr 10 0 do i s>f 2e f/ f. loop cr ; ok
test
0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5
ok
[toc] | [prev] | [next] | [standalone]
| From | beeflo <beeflobill@gmail.com> |
|---|---|
| Date | 2015-05-11 08:57 -0700 |
| Message-ID | <58bb52bd-bc6f-46f0-bb6e-432a2536d571@googlegroups.com> |
| In reply to | #38969 |
On Monday, May 11, 2015 at 9:28:32 AM UTC-6, Paul Rubin wrote: > beeflo <beeflobill@gmail.com> writes: > > I'm struggling to understand how conversion between the parameter > > stack and the floating stack works, using gforth. > > Right now, I'm just trying to convert whole numbers (from a loop > > index) to the floating stack. > > I don't understand what you are trying to do. It sounds like you're > asking for s>f (single to float) since d>f converts a double width integer. > Example (gforth 0.7.0): > > : test cr 10 0 do i s>f 2e f/ f. loop cr ; ok > test > 0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5 > ok That seems like the right word. Thanks. I looked around in the gforth docs, and the only thing I found was d>f, so I thought that was The Way to do it, but I didn't know about double width integers. Maybe I should generally read the ansi docs for reference instead.
[toc] | [prev] | [next] | [standalone]
| From | krishna.myneni@ccreweb.org |
|---|---|
| Date | 2015-05-12 16:36 -0700 |
| Message-ID | <99ac5155-b135-4023-8e19-f6da366d71cf@googlegroups.com> |
| In reply to | #38975 |
On Monday, May 11, 2015 at 10:57:41 AM UTC-5, beeflo wrote: > On Monday, May 11, 2015 at 9:28:32 AM UTC-6, Paul Rubin wrote: > > beeflo <beeflobill@gmail.com> writes: > > > I'm struggling to understand how conversion between the parameter > > > stack and the floating stack works, using gforth. > > > Right now, I'm just trying to convert whole numbers (from a loop > > > index) to the floating stack. > > > > I don't understand what you are trying to do. It sounds like you're > > asking for s>f (single to float) since d>f converts a double width integer. > > Example (gforth 0.7.0): > > > > : test cr 10 0 do i s>f 2e f/ f. loop cr ; ok > > test > > 0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5 > > ok > > That seems like the right word. Thanks. > > I looked around in the gforth docs, and the only thing I found was d>f, so I thought that was The Way to do it, but I didn't know about double width integers. > > Maybe I should generally read the ansi docs for reference instead. For going the other way, ANS Forth provides only F>D, and the spec is worded poorly. The type of truncation performed wasn't clear. After some discussion on c.l.f. many years ago, the appropriate interpretation of F>D was that it should truncate towards zero. Some Forths provide F>S as well, but this is not an ANS Forth word. One should check to see what type of truncation or rounding is performed. In general, it is far more useful to be very explicit in converting from fp to a single number, especially if you want to write portable code. In kForth and PFE, the following words are implemented: FROUND>S -- convert floating point to single integer using rounding. FTRUNC>S -- convert floating point to single using truncation towards zero. One may define these using ANS Forth words in the following way: : FROUND>S FROUND F>D D>S ; : FTRUNC>S F>D D>S ; It is also quite useful to have a built-in non-ANS Forth word, FTRUNC , which does the equivalent of F>D D>F , so that the expensive memory transfers between fp stack and data stack are avoided. I forget now, but I thought we proposed FTRUNC for addition to Forth 200x. Krishna
[toc] | [prev] | [next] | [standalone]
| From | anton@mips.complang.tuwien.ac.at (Anton Ertl) |
|---|---|
| Date | 2015-05-13 16:04 +0000 |
| Message-ID | <2015May13.180441@mips.complang.tuwien.ac.at> |
| In reply to | #39050 |
krishna.myneni@ccreweb.org writes:
>Some Forths provide F>S as well, but this is not an ANS Forth word.
It's Forth-2012.
>It is also quite useful to have a built-in non-ANS Forth word, FTRUNC
Also Forth-2012.
- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: http://www.forth200x.org/forth200x.html
EuroForth 2015: http://www.mpeforth.com/euroforth2015/euroforth2015.htm
[toc] | [prev] | [next] | [standalone]
| From | krishna.myneni@ccreweb.org |
|---|---|
| Date | 2015-05-14 18:41 -0700 |
| Message-ID | <f0c25133-072d-4371-94c9-722c44bf0874@googlegroups.com> |
| In reply to | #39076 |
On Wednesday, May 13, 2015 at 11:06:08 AM UTC-5, Anton Ertl wrote: > krishna.myneni@ccreweb.org writes: > >Some Forths provide F>S as well, but this is not an ANS Forth word. > > It's Forth-2012. > > >It is also quite useful to have a built-in non-ANS Forth word, FTRUNC > > Also Forth-2012. > ... Thanks. My memory was uncertain, after so long. Is there a website for Forth-2012? Krishna
[toc] | [prev] | [next] | [standalone]
| From | anton@mips.complang.tuwien.ac.at (Anton Ertl) |
|---|---|
| Date | 2015-05-15 07:23 +0000 |
| Message-ID | <2015May15.092339@mips.complang.tuwien.ac.at> |
| In reply to | #39128 |
krishna.myneni@ccreweb.org writes:
>On Wednesday, May 13, 2015 at 11:06:08 AM UTC-5, Anton Ertl wrote:
>> krishna.myneni@ccreweb.org writes:
>> >Some Forths provide F>S as well, but this is not an ANS Forth word.
>>
>> It's Forth-2012.
>>
>> >It is also quite useful to have a built-in non-ANS Forth word, FTRUNC
>>
>> Also Forth-2012.
>> ...
>
>Thanks. My memory was uncertain, after so long. Is there a website for Forth-2012?
The standard document is at
http://www.forth200x.org/documents/forth-2012.pdf
http://www.forth200x.org/documents/html/contents.html
- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: http://www.forth200x.org/forth200x.html
EuroForth 2015: http://www.mpeforth.com/euroforth2015/euroforth2015.htm
[toc] | [prev] | [next] | [standalone]
| From | Coos Haak <chforth@hccnet.nl> |
|---|---|
| Date | 2015-05-11 17:43 +0200 |
| Message-ID | <db25x5qm9391$.l4l52mhoyfsh$.dlg@40tude.net> |
| In reply to | #38967 |
Op Mon, 11 May 2015 08:20:27 -0700 (PDT) schreef beeflo: > I'm struggling to understand how conversion between the parameter stack and the floating stack works, using gforth. > > Right now, I'm just trying to convert whole numbers (from a loop index) to the floating stack. > > Through experimentation I've come up with a solution: > >: convert (u u -- ) F:( -- r ) dup 0 < if -1 else 0 then d>f ; > > However, I don't really understand it. > > Why do negative numbers require the -1 to be passed instead of 0? What does the top stack item mean to d>f? > > Is there a better way to do this? > > Where might I read to really get a good handle of floating point representation? > > Gracias. The 'd' in 'd>f' means conversion of a 'double precision integer' to a floating point number. Try this: : cvt cr over . dup . \ print double as two integers d>f fs. \ print floating point number. ; 1000000000000000. cvt -1000000000000000. cvt 100000 0 cvt 100000 -1 cvt 100000 s>d cvt -100000 s>d cvt -- Coos CHForth, 16 bit DOS applications http://home.hccnet.nl/j.j.haak/forth.html
[toc] | [prev] | [next] | [standalone]
| From | beeflo <beeflobill@gmail.com> |
|---|---|
| Date | 2015-05-11 08:58 -0700 |
| Message-ID | <71dff91f-b42c-42bc-a7f5-458b74215598@googlegroups.com> |
| In reply to | #38973 |
On Monday, May 11, 2015 at 9:48:02 AM UTC-6, Coos Haak wrote: > Op Mon, 11 May 2015 08:20:27 -0700 (PDT) schreef beeflo: > > > I'm struggling to understand how conversion between the parameter stack and the floating stack works, using gforth. > > > > Right now, I'm just trying to convert whole numbers (from a loop index) to the floating stack. > > > > Through experimentation I've come up with a solution: > > > >: convert (u u -- ) F:( -- r ) dup 0 < if -1 else 0 then d>f ; > > > > However, I don't really understand it. > > > > Why do negative numbers require the -1 to be passed instead of 0? What does the top stack item mean to d>f? > > > > Is there a better way to do this? > > > > Where might I read to really get a good handle of floating point representation? > > > > Gracias. > > The 'd' in 'd>f' means conversion of a 'double precision integer' to a > floating point number. > Try this: > : cvt cr over . dup . \ print double as two integers > d>f fs. \ print floating point number. > ; > 1000000000000000. cvt > -1000000000000000. cvt > 100000 0 cvt > 100000 -1 cvt > 100000 s>d cvt > -100000 s>d cvt > > -- > Coos > > CHForth, 16 bit DOS applications > http://home.hccnet.nl/j.j.haak/forth.html Oh man, so no matter how big of a number I put in, gforth just splits it up before pushing it onto the stack? I think understanding that was largely at the root of my misunderstanding. Thanks
[toc] | [prev] | [next] | [standalone]
| From | rickman <gnuarm@gmail.com> |
|---|---|
| Date | 2015-05-11 15:09 -0400 |
| Message-ID | <miqunk$bdp$1@dont-email.me> |
| In reply to | #38976 |
On 5/11/2015 11:58 AM, beeflo wrote: > On Monday, May 11, 2015 at 9:48:02 AM UTC-6, Coos Haak wrote: >> Op Mon, 11 May 2015 08:20:27 -0700 (PDT) schreef beeflo: >> >>> I'm struggling to understand how conversion between the parameter stack and the floating stack works, using gforth. >>> >>> Right now, I'm just trying to convert whole numbers (from a loop index) to the floating stack. >>> >>> Through experimentation I've come up with a solution: >>> >>> : convert (u u -- ) F:( -- r ) dup 0 < if -1 else 0 then d>f ; >>> >>> However, I don't really understand it. >>> >>> Why do negative numbers require the -1 to be passed instead of 0? What does the top stack item mean to d>f? >>> >>> Is there a better way to do this? >>> >>> Where might I read to really get a good handle of floating point representation? >>> >>> Gracias. >> >> The 'd' in 'd>f' means conversion of a 'double precision integer' to a >> floating point number. >> Try this: >> : cvt cr over . dup . \ print double as two integers >> d>f fs. \ print floating point number. >> ; >> 1000000000000000. cvt >> -1000000000000000. cvt >> 100000 0 cvt >> 100000 -1 cvt >> 100000 s>d cvt >> -100000 s>d cvt >> >> -- >> Coos >> >> CHForth, 16 bit DOS applications >> http://home.hccnet.nl/j.j.haak/forth.html > > Oh man, so no matter how big of a number I put in, gforth just splits it up before pushing it onto the stack? > > I think understanding that was largely at the root of my misunderstanding. The double precision numbers take two positions on the stack, single precision just one. I assume you understand that the 0 vs. the -1 were for sign extension of the single precision number to make a double precision integer, yes? Or you can use s>d. But as you have found there are both s>f and d>f so you are covered. -- Rick
[toc] | [prev] | [next] | [standalone]
| From | hughaguilar96@gmail.com |
|---|---|
| Date | 2015-05-11 09:21 -0700 |
| Message-ID | <d45ebcc0-759d-4b57-bfaf-a54dc6503be0@googlegroups.com> |
| In reply to | #38967 |
On Monday, May 11, 2015 at 8:20:28 AM UTC-7, beeflo wrote:
> I'm struggling to understand how conversion between the parameter stack and the floating stack works, using gforth.
I have this in the novice package:
http://www.forth.org/novice.html
Here is the code:
0 constant u>d ( u -- d )
macro: u>f ( u -- ) \ float: -- u
u>d d>f ;
macro: s>f ( n -- ) \ float: -- n
s>d d>f ;
[toc] | [prev] | [next] | [standalone]
| From | anton@mips.complang.tuwien.ac.at (Anton Ertl) |
|---|---|
| Date | 2015-05-11 16:55 +0000 |
| Message-ID | <2015May11.185535@mips.complang.tuwien.ac.at> |
| In reply to | #38967 |
beeflo <beeflobill@gmail.com> writes:
>I'm struggling to understand how conversion between the parameter stack and the floating stack works, using gforth.
>
>Right now, I'm just trying to convert whole numbers (from a loop index) to the floating stack.
>
>Through experimentation I've come up with a solution:
>
>: convert (u u -- ) F:( -- r ) dup 0 < if -1 else 0 then d>f ;
>
>However, I don't really understand it.
>
>Why do negative numbers require the -1 to be passed instead of 0? What does the top stack item mean to d>f?
It's the most significant cell of the double-cell integer d. You can
do what you did with "dup 0 < if -1 else 0 then" with S>D. And there
is also S>F ( n -- r ).
I apologize for not documenting S>F; now fixed in the development
version. Thanks for reporting that bug.
- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: http://www.forth200x.org/forth200x.html
EuroForth 2015: http://www.mpeforth.com/euroforth2015/euroforth2015.htm
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.forth
csiph-web