fix some rounding

This commit is contained in:
Alexander Drozdov
2021-10-09 15:01:09 +03:00
parent 8c5470e68a
commit 8fb0dd7cca
4 changed files with 18 additions and 12 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
import { statSourcesTotal, translateStatWithRoll } from '@/parser/modifiers'
import { ParsedItem } from '@/parser/ParsedItem'
import { percentRoll } from '../price-check/filters/util'
import { roundRoll } from '../price-check/filters/util'
export interface PreparedStat {
matcher: string
@@ -14,7 +14,7 @@ export function prepareMapStats (item: ParsedItem): PreparedStat[] {
const prepared = {
matcher: translation.string,
roll: roll && percentRoll(roll.value, 0, Math.floor, translation.dp)
roll: roll && roundRoll(roll.value, translation.dp ?? false)
}
if (translation.negate) {
+2 -2
View File
@@ -13,7 +13,7 @@ import { computed, defineComponent, PropType } from 'vue'
import { useI18n } from 'vue-i18n'
import type { StatCalculated } from '@/parser/modifiers'
import { applyIncr } from '@/parser/advanced-mod-desc'
import { percentRoll } from './util'
import { roundRoll } from './util'
import ItemModifierText from '@/web/ui/ItemModifierText.vue'
export default defineComponent({
@@ -69,7 +69,7 @@ export default defineComponent({
const rollValue = parsed.roll.value * (parsed.translation.negate ? -1 : 1)
return {
text: parsed.translation.string,
roll: percentRoll(rollValue, 0, Math.floor, parsed.roll.dp),
roll: roundRoll(rollValue, parsed.roll.dp),
contribution: contribution!,
contributes: true
}
@@ -1,7 +1,7 @@
import { ParsedItem, ItemRarity, ItemCategory } from '@/parser'
import { ModifierType, StatCalculated, StatRoll, statSourcesTotal, translateStatWithRoll } from '@/parser/modifiers'
import { uniqueModFilterPartial } from './unique-roll'
import { rollToFilter, percentRoll } from './util'
import { rollToFilter, percentRoll, roundRoll } from './util'
import { FilterTag, ItemHasEmptyModifier, StatFilter } from './interfaces'
import { filterPseudo } from './pseudo'
import { filterItemProp } from './pseudo/item-property'
@@ -152,7 +152,7 @@ function itemModFilterPartial (
if (roll) {
if (calc.type === ModifierType.Enchant) {
filter.roll = {
value: percentRoll(roll.value, 0, Math.floor, dp),
value: roundRoll(roll.value, dp),
min: percentRoll(roll.value, 0, Math.floor, dp),
max: percentRoll(roll.value, 0, Math.ceil, dp),
default: {
+12 -6
View File
@@ -1,26 +1,32 @@
import type { StatFilter } from './interfaces'
function showDecimals (value: number, dp: number | boolean): number {
function decimalPlaces (value: number, dp: number | boolean): number {
if (typeof dp === 'number') {
return dp
} else if (!dp || Math.abs(value) > 2) {
} else if (!dp || Math.abs(value) >= 10) {
return 0
} else {
return Math.abs(value) < 1 ? 2 : 1
return Math.abs(value) < 2 ? 2 : 1
}
}
export function roundRoll (value: number, dp: number | boolean) {
const round = Math.pow(10, decimalPlaces(value, dp))
// round value down (toward zero)
return Math.trunc(value * round) / round
}
export function percentRoll (value: number, p: number, method: Math['floor'] | Math['ceil'], dp: number | boolean = false): number {
const res = value + value * p / 100
const rounding = Math.pow(10, showDecimals(value, dp))
const rounding = Math.pow(10, decimalPlaces(value, dp))
return method((res + Number.EPSILON) * rounding) / rounding
}
export function percentRollDelta (value: number, delta: number, p: number, method: Math['floor'] | Math['ceil'], dp = false): number {
const res = value + delta * p / 100
const rounding = Math.pow(10, showDecimals(value, dp))
const rounding = Math.pow(10, decimalPlaces(value, dp))
return method((res + Number.EPSILON) * rounding) / rounding
}
@@ -34,7 +40,7 @@ export function rollToFilter (
// disabled by default, so opts.neverNegated acts more like
// acknowledgment of what you are doing
return {
value: percentRoll(roll, 0, Math.floor, dp),
value: roundRoll(roll, dp ?? false),
default: {
min: percentRoll(roll, -percent * Math.sign(roll), Math.floor, dp),
max: percentRoll(roll, +percent * Math.sign(roll), Math.ceil, dp)