Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/concepts/floating point"

From cppreference.com
< cpp‎ | concepts
m (Added Spanish language link)
(+References)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{cpp/title|floating_point}}
+
{{cpp/title|floating_point {{mark since c++20}}}}
 
{{cpp/concepts/navbar}}
 
{{cpp/concepts/navbar}}
 
{{ddcl|header=concepts|since=c++20|1=
 
{{ddcl|header=concepts|since=c++20|1=
template < class T >
+
template< class T >
 
concept floating_point = std::is_floating_point_v<T>;
 
concept floating_point = std::is_floating_point_v<T>;
 
}}
 
}}
Line 8: Line 8:
 
The concept {{c|floating_point<T>}} is satisfied if and only if {{tt|T}} is a floating-point type.
 
The concept {{c|floating_point<T>}} is satisfied if and only if {{tt|T}} is a floating-point type.
  
=== See also ===
+
===Example===
 +
{{example
 +
|code=
 +
#include <concepts>
 +
#include <iostream>
 +
#include <type_traits>
 +
 
 +
constexpr std::floating_point auto x2(std::floating_point auto x)
 +
{
 +
    return x + x;
 +
}
 +
 
 +
constexpr std::integral auto x2(std::integral auto x)
 +
{
 +
    return x << 1;
 +
}
 +
 
 +
int main()
 +
{
 +
    constexpr auto d = x2(1.1);
 +
    static_assert(std::is_same_v<double const, decltype(d)>);
 +
    std::cout << d << '\n';
 +
 
 +
    constexpr auto f = x2(2.2f);
 +
    static_assert(std::is_same_v<float const, decltype(f)>);
 +
    std::cout << f << '\n';
 +
 
 +
    constexpr auto i = x2(444);
 +
    static_assert(std::is_same_v<int const, decltype(i)>);
 +
    std::cout << i << '\n';
 +
}
 +
|output=
 +
2.2
 +
4.4
 +
888
 +
}}
 +
 
 +
===References===
 +
{{ref std c++23}}
 +
{{ref std|section=18.4.7|title=Arithmetic concepts|id=concepts.arithmetic}}
 +
{{ref std end}}
 +
{{ref std c++20}}
 +
{{ref std|section=18.4.7|title=Arithmetic concepts|id=concepts.arithmetic}}
 +
{{ref std end}}
 +
 
 +
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/types/dsc is_floating_point}}
+
{{dsc inc|cpp/types/dsc is_floating_point}}
{{dsc end }}
+
{{dsc end}}
  
{{langlinks|es|ja|zh}}
+
{{langlinks|de|es|ja|ru|zh}}

Latest revision as of 10:13, 7 September 2024

Defined in header <concepts>
template< class T >
concept floating_point = std::is_floating_point_v<T>;
(since C++20)

The concept floating_point<T> is satisfied if and only if T is a floating-point type.

[edit] Example

#include <concepts>
#include <iostream>
#include <type_traits>
 
constexpr std::floating_point auto x2(std::floating_point auto x)
{
    return x + x;
}
 
constexpr std::integral auto x2(std::integral auto x)
{
    return x << 1;
}
 
int main()
{
    constexpr auto d = x2(1.1);
    static_assert(std::is_same_v<double const, decltype(d)>);
    std::cout << d << '\n';
 
    constexpr auto f = x2(2.2f);
    static_assert(std::is_same_v<float const, decltype(f)>);
    std::cout << f << '\n';
 
    constexpr auto i = x2(444);
    static_assert(std::is_same_v<int const, decltype(i)>);
    std::cout << i << '\n';
}

Output:

2.2
4.4
888

[edit] References

  • C++23 standard (ISO/IEC 14882:2024):
  • 18.4.7 Arithmetic concepts [concepts.arithmetic]
  • C++20 standard (ISO/IEC 14882:2020):
  • 18.4.7 Arithmetic concepts [concepts.arithmetic]

[edit] See also

checks if a type is a floating-point type
(class template) [edit]