# Using `kernel_float::constant` When working with mixed precision types, you will find that working with constants presents a bit of a challenge. For example, a simple expression such as `3.14 * x` where `x` is of type `vec` will NOT be performed in `float` precision as you might expect, but instead in `double` precision. This happens since the left-hand side of this expression (a constant) is a `double` and thus `kernel_float` will also cast the right-hand side to `double`. To solve this problem, `kernel_float` offers a type called `constant` that can be used to represent constants. Any binary operations between a value of type `U` and a `constant` will result in both operands being cast to type `U` and the operation is performed in the precision of type `U`. This makes `constant` useful for representing constants in your code. To see the effect in isolation, compare the deduced type of `a` and `b` below: ```cpp kf::vec x = {1.0f, 2.0f}; // Without `constant`: `3.14` is a `double`, so `x` is promoted to `double` and the // multiplication happens in `double` precision. kf::vec a = 3.14 * x; // With `constant`: the `double` literal is wrapped in `constant`, so the multiplication // is performed directly in `float` precision. kf::vec b = kf::make_constant(3.14) * x; ``` Now consider a larger, more realistic example that puts this into practice: ``` #include "kernel_float.h" namespace kf = kernel_float; int main() { using Type = float; const int N = 8; static constexpr auto PI = kf::make_constant(3.14); kf::vec i = kf::range(); kf::vec x = kf::cast(i) * PI; kf::vec y = x * kf::sin(x); Type result = kf::sum(y); printf("result=%f", double(result)); return EXIT_SUCCESS; } ``` This code example uses the ``make_constant`` utility function to create `constant`.