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.
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 .
To convert an integer data type to float you can wrap the integer with float64() or float32.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With