我不擅长str和int之间的转换。
#pragma once
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cstdint>
#include <exception>
#include <iterator>
#include <iostream>
#include <iomanip>
#include <limits>
#include <type_traits>
namespace vpc {
using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
template<typename T>
struct Register;
using Reg8 = Register<u8>;
using Reg16 = Register<u16>;
using Reg32 = Register<u32>;
using Reg64 = Register<u64>;
template<typename T>
struct Register {
T value;
std::bitset<sizeof(T)* CHAR_BIT> bits;
Register() : value{ 0 }, /*previous_value{ 0 },*/ bits{ 0 } {}
template<typename U, std::enable_if_t<(sizeof(U) > sizeof(T))>* = nullptr>
explicit Register(const U val, const u8 idx = 0) :
value{ static_cast<T>((val >> std::size(bits) * idx) &
std::numeric_limits<std::make_unsigned_t<T>>::max()) },
bits{ value }
{
constexpr u16 sizeT = sizeof(T);
constexpr u16 sizeU = sizeof(U);
assert((idx >= 0) && (idx <= ((sizeU / sizeT) - 1)) );
}
template<typename U, std::enable_if_t<(sizeof(U) < sizeof(T))>* = nullptr>
explicit Register(const U val, const u8 idx = 0) :
value{ static_cast<T>((static_cast<T>(val) << sizeof(U)*CHAR_BIT*idx) &
std::numeric_limits<std::make_unsigned_t<T>>::max()) },
bits{ value }
{
constexpr u16 sizeT = sizeof(T);
constexpr u16 sizeU = sizeof(U);
assert((idx >= 0) && (idx <= ((sizeT / sizeU) - 1)) );
}
template<typename U, std::enable_if_t<(sizeof(U) == sizeof(T))>* = nullptr>
explicit Register(const U val, const u8 idx = 0) :
value{ static_cast<T>( val ) }, bits{ value }
{}
template<typename... Args>
Register(Args... args) {}
template<typename U>
Register(const Register<U>& reg, const u8 idx = 0) : Register(reg.value, idx) {}
void changeEndian() {
T tmp = value;
char* const p = reinterpret_cast<char*>(&tmp);
for (size_t i = 0; i < sizeof(T) / 2; ++i)
std::swap(p[i], p[sizeof(T) - i - 1]);
bits = tmp;
}
Register& operator=(const Register& obj) {
this->value = obj.value;
//this->previous_value = obj.previous_value;
this->bits = obj.bits;
return *this;
}
template<typename Lhs, typename Rhs>
friend auto operator+(const Register<Lhs>& l, const Register<Rhs>& r);
template<typename Lhs, typename Rhs>
friend auto operator-(const Register<Lhs>& l, const Register<Rhs>& r);
template<typename Lhs, typename Rhs>
friend auto operator*(const Register<Lhs>& l, const Register<Rhs>& r);
template<typename Lhs, typename Rhs>
friend auto operator/(const Register<Lhs>& l, const Register<Rhs>& r);
template<typename Lhs, typename Rhs>
friend auto operator%(const Register<Lhs>& l, const Register<Rhs>& r);
template<typename Lhs, typename Rhs>
friend auto operator&(const Register<Lhs>& l, const Register<Rhs>& r);
template<typename Lhs, typename Rhs>
friend auto operator|(const Register<Lhs>& l, const Register<Rhs>& r);
template<typename Lhs, typename Rhs>
friend auto operator^(const Register<Lhs>& l, const Register<Rhs>& r);
template<typename Reg>
friend auto operator~(const Register<Reg>& l);
};
template<typename Lhs, typename Rhs>
auto operator+(const Register<Lhs>& l, const Register<Rhs>& r) {
return Register<decltype(l.value + r.value)>{ l.value + r.value };
}
template<typename Lhs, typename Rhs>
auto operator-(const Register<Lhs>& l, const Register<Rhs>& r) {
return Register<decltype(l.value - r.value)>{ l.value - r.value };
}
template<typename Lhs, typename Rhs>
auto operator*(const Register<Lhs>& l, const Register<Rhs>& r) {
return Register<decltype(l.value * r.value)>{ l.value * r.value };
}
template<typename Lhs, typename Rhs>
auto operator/(const Register<Lhs>& l, const Register<Rhs>& r) {
if (r.value == 0)
throw std::exception( "Division by 0\n" );
return Register<decltype(l.value / r.value)>{ l.value / r.value };
}
template<typename Lhs, typename Rhs>
auto operator%(const Register<Lhs>& l, const Register<Rhs>& r) {
return Register<decltype(l.value % r.value)>{ l.value % r.value };
}
template<typename Lhs, typename Rhs>
auto operator&(const Register<Lhs>& l, const Register<Rhs>& r) {
return Register<decltype(l.value & r.value)>{ l.value & r.value};
}
template<typename Lhs, typename Rhs>
auto operator|(const Register<Lhs>& l, const Register<Rhs>& r) {
return Register<decltype(l.value | r.value)>{ l.value | r.value};
}
template<typename Lhs, typename Rhs>
auto operator^(const Register<Lhs>& l, const Register<Rhs>& r) {
return Register<decltype(l.value ^ r.value)>{ l.value ^ r.value };
}
template<typename Reg>
auto operator~(const Register<Reg>& r) {
return Register<decltype(~r.value)>{~r.value};
}
template<typename T>
std::ostream& operator<<(std::ostream& os, const Register<T>& r) {
return os << "Reg" << std::size(r.bits) << '(' << +r.value << ")"
<< "\nhex: 0x" << std::uppercase << std::setfill('0') << std::setw(sizeof(T) * 2) << std::hex
<< +r.bits.to_ullong() << std::dec << "\nbin: "
<< r.bits << "\n\n";
}
template<typename T>
T changeEndian(T in) {
char* const p = reinterpret_cast<char*>(&in);
for (size_t i = 0; i < sizeof(T) / 2; ++i)
std::swap(p[i], p[sizeof(T) - i - 1]);
return in;
}
template<typename T>
Register<T> reverseBitOrder(Register<T>& reg, bool copy = false) {
static constexpr u16 BitCount = sizeof(T) * CHAR_BIT;
auto str = reg.bits.to_string();
std::reverse(str.begin(), str.end());
if (copy) { // return a copy
Register<T> cpy;
cpy.bits = std::bitset<BitCount>(str);
cpy.value = static_cast<T>(cpy.bits.to_ullong());
return cpy;
}
else {
reg.bits = std::bitset<BitCount>(str);
//reg.previous_value = reg.value;
reg.value = static_cast<T>(reg.bits.to_ullong());
return {};
}
}
} // namespace vpc
答案 0 :(得分:2)
您的错误与转换类型无关
if x > x_high
,那么您永远不会为loss
赋值,但是您正在检查if loss < max_loss
,并且loss
不存在
您应该在for循环之前为其分配一些默认值,就像您为最大损失所做的那样
此外,您有一个无限的while循环,因为len(stock_value) < number_of_stocks
始终为true。股票的长度或数量改变价值