7

C has data types, so if you add an int to a float, for example:

int i = 1;
float f = 2.5;
float r = i + f;

The C compiler will know that it should generate machine instructions that adds an int to a float because the C compiler can see that i is an int and f is a float.


But there is no data types in the B programming language, so if we did the following (not sure if this is a correct syntax):

auto i = 1;
auto f = 2.5;
auto r = i + f;

How would the B compiler know that it should generate machine instructions that adds an int to a float?

3
  • Do you have a reference that says constants can be formed using a decimal point? The grammar (bell-labs.com/usr/dmr/www/kbman.html) I'm looking at doesn't allow . in numbers, which tells me that B doesn't have a notion of floating point types.
    – Erik Eidt
    Apr 24, 2019 at 1:24
  • @Erik Eidt Oh! sorry, I assumed that float literals are supported.
    – user13423
    Apr 24, 2019 at 1:56
  • 2
    As a general observation, if the language is typeless, then an operator-symbol denotes a single operation (where in a typed language '+' for example denotes a number of different-but-similar operations), and it devolves to the programmer to select the appropriate operator to achieve his desired effect. Apr 24, 2019 at 10:07

1 Answer 1

14

B was a typeless language so i + f was always an integer addition operation. As it was typeless this also means the value in f wouldn't be converted to integer, it was just assumed to already be an integer. The bits stored in f would give it its value.

However the original B implementation didn't support floating point, so there was no way to even have floating point literals. There was at least one implementation of B, for GCOS8, where you could in fact do f = 2.5;, but i + f still worked as described above. If you wanted to do a floating point addition instead you'd do i #+ f, but that had the same problem. It assumed i was already a floating point value and wouldn't convert it.

So to get the same behaviour as in C, in addition to using the #+ floating point addition operator, you'd have to explicitly convert i to a floating point value:

auto r;
r = #i #+ f;

Where # is the unary operator that converts integer values to floating point. B also doesn't seem to have supported initialization of auto variables, so two statements are necessary here.

2
  • So, is i #+ f basically equivalent to C's *(float*)&i + *(float*)&f?
    – Ruslan
    Apr 19, 2020 at 13:10
  • @Ruslan Yah, it ignores whatever types i and f have, because B doesn't have types, and just interprets i and f as containing floating point values.
    – user722
    Apr 19, 2020 at 15:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .