Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to floating point numbers in the absence of an FPU?

If you are programming with the C language for a microprocessor that does not have an FPU, does the compiler signal errors when floating point literals and keywords are encountered (0.75, float, double, etc)?

Also, what happens if the result of an expression is fractional?

I understand that there are software libraries that are used so you can do floating-point math, but I am specifically wondering what the results will be if you did not use one.

Thanks.

like image 726
jmrah Avatar asked Dec 11 '22 07:12

jmrah


1 Answers

A C implementation is required to implement the types float and double, and arithmetic expressions involving them. So if the compiler knows that the target architecture doesn't have floating-point ops then it must bring in a software library to do it. The compiler is allowed to link against an external library, it's also allowed to implement floating point ops in software by itself as intrinsics, but it must somehow generate code to get it done.

If it doesn't do so [*] then it is not a conforming C implementation, so strictly speaking you're not "programming with the C language". You're programming with whatever your compiler docs tell you is available instead.

You'd hope that code involving float or double types will either fail to compile (because the compiler knows you're in a non-conforming mode and tells you) or else fails to link (because the compiler emits calls to emulation routines in the library, but the library is missing). But you're on your own as far as C is concerned, if you use something that isn't C.

I don't know the exact details (how old do I look?), but I imagine that back in the day if you took some code compiled for x87 then you might be able to link and load it on a system using an x86 with no FPU. Then the CPU would complain about an illegal instruction when you tried to execute it -- quite possibly the system would hang depending what OS you were running. So the worst possible case is pretty bad.

what happens if the result of an expression is fractional?

The actual result of an expression won't matter, because the expression itself was either performed with integer operations (in which case the result is not fractional) or else with floating-point operations (in which case the problem arises before you even find out the result).

[*] or if you fail to specify the options to make it do so ;-)

like image 183
Steve Jessop Avatar answered Dec 13 '22 20:12

Steve Jessop