Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix Map function - IntegerDivideByZero #8938#8957

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
TD-er wants to merge3 commits intoesp8266:master
base:master
Choose a base branch
Loading
fromTD-er:bugfix/8938_MapFunction
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 71 additions & 4 deletionscores/esp8266/WMath.cpp
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -72,11 +72,78 @@ long secureRandom(long howsmall, long howbig) {
}

long map(long x, long in_min, long in_max, long out_min, long out_max) {
const long dividend = out_max - out_min;
const long divisor = in_max - in_min;
const long delta = x - in_min;
long in_length = in_max - in_min;
long out_length = out_max - out_min;

return (delta * dividend + (divisor / 2)) / divisor + out_min;
if (in_length == 0 || out_length == 0) {
return out_min;
}

long delta = x - in_min;

if ((out_length < 0) && (in_length < 0)) {
std::swap(in_min, in_max);
std::swap(out_min, out_max);
} else if (out_length < 0) {
x = in_max - delta;
std::swap(out_min, out_max);
} else if (in_length < 0) {
x = in_max - delta;
std::swap(in_min, in_max);
}

// Update length and delta as in/out values may have changed.
delta = x - in_min;
in_length = in_max - in_min;
out_length = out_max - out_min;

if (out_length == in_length) {
return out_min + delta;
}

// We now know in_min < in_max and out_min < out_max
// Make sure x is within range of in_min ... in_max
// Shift the in/out range to contain 'x'
if ((x < in_min) || (x > in_max)) {
long shift_factor = 0;

if (x < in_min) {
const long before_min = in_min - x;
shift_factor = -1 - (before_min / in_length);
} else {
// x > in_max
const long passed_max = x - in_max;
shift_factor = 1 + (passed_max / in_length);
}

const long in_shift = shift_factor * in_length;
const long out_shift = shift_factor * out_length;
in_min += in_shift;
in_max += in_shift;
out_min += out_shift;
out_max += out_shift;
delta = x - in_min;
}

if (out_length > in_length) {
// Map to larger range
// Do not 'simplify' this calculation
// as this will result in integer overflow errors
const long factor = out_length / in_length;
const long error_mod = out_length % in_length;
const long error = (delta * error_mod) / in_length;
return (delta * factor) + out_min + error;
}

// abs(out_length) < abs(in_length)
// Map to smaller range
// Do not 'simplify' this calculation
// as this will result in integer overflow errors
const long factor = (in_length / out_length);
const long estimate_full = in_length / factor + out_min;
const long error = (delta * (out_max - estimate_full)) / in_length;

return delta / factor + out_min + error;
}

uint16_t makeWord(uint16_t w) {
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp