Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #38985

Re: How to convert whole numbers to floating?

From rickman <gnuarm@gmail.com>
Newsgroups comp.lang.forth
Subject Re: How to convert whole numbers to floating?
Date 2015-05-11 15:09 -0400
Organization A noiseless patient Spider
Message-ID <miqunk$bdp$1@dont-email.me> (permalink)
References <765d92cc-590c-4da4-a0af-65f9116fbf52@googlegroups.com> <db25x5qm9391$.l4l52mhoyfsh$.dlg@40tude.net> <71dff91f-b42c-42bc-a7f5-458b74215598@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web