Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add an integer value to a floating point value in Gforth

Tags:

forth

gforth

In Gforth, is there a way to add an integer value to a floating point value?

Something like 1 + 2.1? If I do 1 2.1e f+ I get an error which I'm guessing is because the values are not on the same stack. I know that I could just do 1.0e 2.1e f+, but that's not what I'm trying to figure out how to do.

like image 749
user1981802 Avatar asked Jan 15 '13 22:01

user1981802


People also ask

Can you add an int to a float?

Yes, an integral value can be added to a float value. The basic math operations ( + , - , * , / ), when given an operand of type float and int , the int is converted to float first. So 15.0f + 2 will convert 2 to float (i.e. to 2.0f ) and the result is 17.0f .

How do you float an integer?

To convert an integer data type to float you can wrap the integer with float64() or float32.


1 Answers

Gforth has the s>f and d>f words that convert an int (single cell and double cell respectively) to a double - Gforth floating point functions doc is here

1 s>f 2.1e f+

should do the trick in this case.

like image 133
fvu Avatar answered Sep 30 '22 01:09

fvu