Why does subtraction of float by float give unexpected result?

Asked

Viewed 82 times

2

I spent a good time to solve this problem on my system. Solved? I did, but at some point it’s working (the subtraction value) = -0.0 and I have to give . replace('-', '') to show the user a positive value and this causes a lapse of change that is noticeable. All this is happening due to subtracting 0.1 from 2.8 results in 2.6999999999997. In my code it is already used . toFixed(1) and still won’t, I’ve even used Math.round().

After all, what is the point of subtracting 0.1 from 2.8 and resulting in that?

@Edit

I warn people that they "parachuted down". This issue is not duplicated either has nothing to do with the topic indicated, because my question was something very specific that, just looking at the code in the jsfiddle left in the comments, is that a solution can come to your problem.

  • Worse than I’ve ever read this topic and several others and, as I said, I’m already using .toFixed. I’ve cooled my head for a while and came up with some ideas. Thanks for trying to help.

  • Nah. It wasn’t, I’ll leave it at that. I got beat xD

  • The bid is to work with toFixed(n)... the sums and subtractions will always be exact.

  • Worse than being correctly added and subtracted, it is not showing values beyond a decimal place (.toFixed(1)). The problem is that when it reaches 0.0, it’s showing -0.0 and I have to use replace to get that negative. What it looks like is like . toFixed was just masking and, from behind, the value is all broken and showing that negative. Unfortunately we can’t do everything the way we want.

  • Something is wrong. In what situation -?

  • https://jsfiddle.net/h4dept29/ . The idea is what’s in this fiddle. Being that by my code there are several checks to see whether or not the user can increase or decrease. The fiddle is correct and does not appear -0.0, which I was surprised, because the code that is in the fiddle is identical. Now, I swear I can’t imagine what it could be. However, it already rules out the possibility of code error.

  • I was able to replicate the error. Please click on the + until 0.0. https://jsfiddle.net/h4dept29/3/

Show 2 more comments

1 answer

2


Have to convert toFixed(1) and in parseFloat() then. You will not be able to use the operator -= nor the += because of the problem with the float.

Run the snippet and click + over and over until 0:

var $menosPontoVelocidade = '#menos-ponto',
	$addPontoVelocidade = '#add-ponto',
	$velocidadeId = '#velocidade',
	$podeAdicionarHabilidades = true,
	$podeRetirarHabilidades = false,
	$podeRetirarHabilidadeVelocidade = false,
	$pontosDistribuirHabilidades = 2,
	$pontosDistribuirHabilidadesId = '#pontos-distribuir';


$(function($) {
	$($menosPontoVelocidade).on('click', function() {
			if ($podeRetirarHabilidades && $podeRetirarHabilidadeVelocidade) {
				retirar_pontos_habilidades($velocidadeId);
			}
		});
		
	$($addPontoVelocidade).on('click', function() {
		if ($podeAdicionarHabilidades) {
			adicionar_pontos_habilidades($velocidadeId);
		}
	});
	
	
	function retirar_pontos_habilidades($inputId) {
		$pontosDistribuirHabilidades += 0.1;
		$($pontosDistribuirHabilidadesId).text($pontosDistribuirHabilidades.toFixed(1));
		var $str = (parseFloat($($inputId).val()) - 0.1).toFixed(1);
		$($inputId).val($str);
	}

function adicionar_pontos_habilidades($inputId) {
		$pontosDistribuirHabilidades = parseFloat(($pontosDistribuirHabilidades-0.1).toFixed(1));
		$($pontosDistribuirHabilidadesId).text($pontosDistribuirHabilidades.toFixed(1));
		var $str = (parseFloat($($inputId).val()) + 0.1).toFixed(1);
		$($inputId).val($str);
	}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="menos-ponto"> - </span>
<input type="text" size="5" id="velocidade" disabled value="0.0" />
<span id="add-ponto"> + </span>
<p id="pontos-distribuir">2.0</p>

  • It’s that kind of situation where you’re floating around to understand and you wouldn’t even think it would be like this. Therefore, I can only "decorate" and try to apply in the future when necessary. If it were not for your experience, I would not have solved. Thank you for your kind attention :)

  • Imagine friend! We learn together. Abs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.