C++Guns – RoboBlog blogging the bot

29.07.2019

C++ Guns: P1161R3 Deprecate uses of the comma operator in subscripting expressions

Filed under: Allgemein — Tags: — Thomas @ 14:07

Endlich.

ND Arrays in std C++ wir kommen

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1161r3.html

Don't forget GCC -Werror=unused-value

array[x]      // Ok
array[(x,y)]  // Ok, uses y as index/key
array[x,y]    // Deprecated, uses y as index/key

08.07.2019

C++ Guns: std::ofstream better err msg

Filed under: Allgemein — Tags: — Thomas @ 20:07

Folgender Fetzen Code gibt eine gescheite Fehlermeldung für streams (wann fixen die endlich streams & exceptions?)

    auto fileName = std::string(getenv("HOME")) + "/config/clusterprocessaccounting.dat";
    std::ofstream f(fileName);
    if(not f) throw std::system_error(errno, std::system_category(), "failed to open " + fileName + " ");

terminate called after throwing an instance of 'std::system_error'
what(): failed to open /home/ite/config/clusterprocessaccounting.dat : No such file or directory

04.07.2019

C++ Guns: Fingerübung: can convert double to int?

Filed under: Allgemein — Tags: — Thomas @ 09:07

Wir bekommen öfter mal große integer Zahlen als double gespeichert in den Daten. Beim konvertieren nach Text (andere story) mit 6 signifikanten Ziffern und wissenschaftlicher Darstellung fallen Ziffern weg. Obwohl die wissenschaftliche Darstellung mit einem Exponent bei ganzen Zahlen nicht nötig wäre.

Fingerübung: Schreibe eine Funktion die true zurück gibt, wenn die übergebene double Zahl in ein 32bit signed integer, ohne Verluste, passt.

Da es UB ist, eine zu große double Zahl einem int zuzuweisen (mit constexpr überprüft), ist die Aufgabe nicht ganz so trivial.

constexpr int xxx = std::numeric_limits<int>::max()+1.0; // error: overflow in constant expression

Noch dazu kann ein Integer mehr negative Zahlen als positive Speichern (Die Null zähl mit). Und es gibt natürlich noch Inf und Nan.
Nach drei Versuchen sieht der Code nun wie folgt aus. constexpr Test inklusive.

#include <limits>
#include <cmath>

constexpr inline bool canConvertToInt(double d) {
  if(std::isnan(d) or std::isinf(d)) return false;
  if(d > 0) {
    return d <= std::numeric_limits<int>::max() and d-int(d)==0;
  } else {
    return d >= std::numeric_limits<int>::min() and d-int(d)==0;
  }
}

  static_assert(canConvertToInt(std::numeric_limits<int>::max()-1));
  static_assert(not canConvertToInt(std::numeric_limits<int>::max()-1 + 0.5));
  static_assert(canConvertToInt(std::numeric_limits<int>::max()));
  static_assert(not canConvertToInt(std::numeric_limits<int>::max()+0.5));

  static_assert(canConvertToInt(std::numeric_limits<int>::min()+1));
  static_assert(not canConvertToInt(std::numeric_limits<int>::min()+1-0.5));
  static_assert(canConvertToInt(std::numeric_limits<int>::min()));
  static_assert(not canConvertToInt(std::numeric_limits<int>::min()-0.5));

  static_assert(not canConvertToInt(std::numeric_limits<double>::infinity()));
  static_assert(not canConvertToInt(std::numeric_limits<double>::quiet_NaN()));
  static_assert(not canConvertToInt(std::numeric_limits<double>::signaling_NaN()));
  static_assert(not canConvertToInt(std::numeric_limits<double>::denorm_min()));

Der Praxistest kann beginnen.

Powered by WordPress