[erlang-questions] Strange float / tuple problem

Donald Steven t6sn7gt@REDACTED
Mon Jun 6 16:02:51 CEST 2016


This is most helpful.   Thanks Craig.

Don

On 06/05/2016 12:25 AM, zxq9 wrote:
> On 2016年6月5日 日曜日 13:11:30 zxq9 wrote:
>> On 2016年6月4日 土曜日 10:07:11 Donald Steven wrote:
>>> Thanks Matthias and Craig.  I see the issue and I've got a work around.
>>> BTW, is there a way to truncate a float to a given # of places, not just
>>> 0 -- not just a formatted output, but a true truncation?
>> approximate(Precision) ->
>>      fun(Z, Z) -> true;
>>         (Z, Q) -> (Z - Precision =< Q) and (Q =< Z + Precision)
>>      end.
> I forgot to mention something... in your particular case you may really
> be wanting to convert to a specific value instead of just compare, in that
> case a small change is more direct:
>
> force_fit(Precision) ->
>      Approx = approximate(Precision),
>      fun(Z, Q) ->
>          case Approx(Z, Q) of
>              true  -> Z;
>              false -> Q
>          end
>      end.
>
> 1> Rough = numbers:force_fit(0.00001).
> #Fun<numbers.1.117927507>
> 2> Rough(0.0, 4.440892098500626e-16).
> 0.0
> 3> Rough(0.0, 0.1).
> 0.1
>
> Obviously this would be playing with fire on intermediate values (*much*
> more dangerous than the usual caveats that apply to iterative processing
> with floats). But it may be closer to the behavior you are looking for --
> conceptually, anyway. I have no idea what you're really doing.
>
> -Craig
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list