Skip to content

Commit

Permalink
解决AVR架构log2(0)出错问题
Browse files Browse the repository at this point in the history
  • Loading branch information
埃博拉酱 committed Jan 14, 2025
1 parent e733b7d commit 71b57d2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions examples/Functional/Functional.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void Translate(std::chrono::hours H) {
std::cout << std::chrono::duration_cast<std::chrono::seconds>(H + H).count() << std::endl;
}
std::move_only_function<void()> FF;
static volatile const float LogMin = std::log2(0);
void setup() {
std::unique_ptr<std::chrono::hours[]> US = std::make_unique_for_overwrite<std::chrono::hours[]>(10);
std::shared_ptr<std::chrono::hours[]> SSS;
Expand Down
10 changes: 6 additions & 4 deletions src/cmath
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#ifdef ARDUINO_ARCH_AVR
/* Copyright (C) 2006 Garrett A. Kajmowicz

This file is part of the uClibc++ Library.
Expand All @@ -18,6 +17,7 @@
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef ARDUINO_ARCH_AVR
#include <math.h>
#include "type_traits"
#include "ext/type_traits.h"
Expand Down Expand Up @@ -45,9 +45,11 @@
#undef tan
#undef tanh
#else
#include "Cpp_Standard_Library.h"
#include_next <cmath>
#endif
#ifdef ARDUINO_ARCH_SAM
#include <limits>
#endif
namespace std
{
#ifdef ARDUINO_ARCH_AVR
Expand Down Expand Up @@ -300,10 +302,10 @@ namespace std

template <typename _Tp>
constexpr typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
double>::__type
double>::__type
log2(_Tp __x)
{
return __builtin_log2(__x);
return __x ? __builtin_log2(__x) : -std::numeric_limits<double>::infinity(); // AVR-GCC 7.3.0 的__builtin_log2输入0会出错,而不是返回负无穷
}
#endif
}

0 comments on commit 71b57d2

Please sign in to comment.