Skip to content

c++ comparison between signed and unsigned, types cast with boundary check

License

Notifications You must be signed in to change notification settings

narizhny/numeric_safety

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Numeric cast

Library for fast answering frequent interview questions

Cast numeric

#include "numeric_cast.h"
uint32_t a1 = 4000000000;
uint32_t b1 = 10;
std::cout << numeric_cast<int32_t>(a1) << std::endl; // throws std::overflow_error
std::cout << numeric_cast<char16_t>(b1) << std::endl; // Ok

Signed/unsigned comparison

#include <stdint.h>
#include "compare.h"

int32_t a = -123;
uint32_t b = 4000000000;
std::cout << std::boolalpha << less(a, b) << std::endl; //true

Floating point values comparison

#include "floating_point_compare.h"
double d1 = 8.0000001;
double d2 = 8.0000005;

std::cout << std::boolalpha << floating_point_compare(d1, d2) << std::endl;     //false
std::cout << std::boolalpha << floating_point_compare<4>(d1, d2) << std::endl;	//true

About

c++ comparison between signed and unsigned, types cast with boundary check

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages