5 ms·
What if you needed to use complex numbers, and your language only supports ints and floats? Is the library or class used to implement the complex data type just
by goatlover 10y ago
What if you needed to use complex numbers, and your language only supports ints and floats? Is the library or class used to implement the complex data type just a bundle of existing types?
- problems 10y agoYes. There aren't to my knowledge any CPUs with special instructions for complex numbers, they're implemented as wrappers ontop of the standard stuff.
- goatlover 10y agoIf we're talking about CPUs, then structures and functions are just bundles of existing bit patterns and machine instructions. Reducing things to just their primitives ignores how useful abstractions are for humans. Not everyone thinks it's a good use of their time to reinvent classes & objects or closures with structures and function pointers. Thus why those abstractions are part of so many programming languages (to answer the expressed bafflement one level above my initial reply). Some C or assembly fans act like abstractions should end at that level, for some strange reason.
- problems 10y agoI didn't say anything about it not being a useful abstraction, just that it is essentially just an abstraction.
- deleted 10y ago[deleted]
- weinzierl 10y agoARM has special instructions for complex numbers[1]. Modern CPU architectures usually have vector instructions which should cover most common complex number operations as well. [1] Look for the instructions prefixed with FC = Floating-point Complex https://developer.arm.com/docs/dui0801/latest/a64-simd-vector-instructions https://developer.arm.com/docs/dui0801/latest/a64-simd-vecto...
- deleted 10y ago[deleted]
- humanrebar 10y ago> There aren't to my knowledge any CPUs with special instructions for complex numbers There are lots of processors out there besides CPUs. You could even design a new processor or program an FPGA to do complex math. Fast Fourier transforms deal with imaginary math, for example. You can do those in a number of ways, including on a CPU or GPU, but it's not unheard of for companies to use either custom hardware or programmable hardware to meet performance needs.
- gpderetta 10y agoIntel CPUs have specific vector instructions specifically meant to accelerate operations on complex numbers (ADDSUBPS for example).
- taneq 10y agoWell, yeah. Usually somewhere in there it'll have class Complex { double real; double imaginary; } Or similar. Maybe template it so you can have a Complex<double> instead. It's still fundamentally a combination of existing types.
- goatlover 10y agoThat doesn't change the usefulness of being able to create new data types from existing ones. With operator overloading or operators as methods, you can treat user your new data types as primitives. That's very convenient when you want your complex numbers to work just like ints and floats.
- taneq 10y agoI didn't mean to say it's not useful, just that it's much simpler than actually defining a new primitive type, which is how I originally interpreted the term.