Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
OtpErlangDouble/OtpErlangFloat
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Martin Dimitrov  
View profile  
 More options Aug 8 2012, 7:01 am
From: Martin Dimitrov <mrtndimit...@gmail.com>
Date: Wed, 08 Aug 2012 14:01:21 +0300
Local: Wed, Aug 8 2012 7:01 am
Subject: [erlang-questions] OtpErlangDouble/OtpErlangFloat
Hello,

I have some strings in Java representing floating point numbers. I want
to transfer them to Erlang. So I create an object of type
OtpErlangFloat. The problem is that is rounds the number funnily. For
example,

String s = "22.4";
float f = Float.parseFloat(s);
System.out.println(f); // 22.4
OtpErlangFloat ef = new OtpErlangFloat(f);
System.out.println(ef.toString()); // 22.399999618530273

Then in Erlang:

mochinum:digits(Ef).
"22.399999618530273"

but

mochinum:digits(22.4).
"22.4"

Is there a way to keep the precision when constructing
OtpErlangDouble/OtpErlangFloat?

Thanks in advance,

Martin

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard O'Keefe  
View profile  
 More options Aug 8 2012, 6:41 pm
From: "Richard O'Keefe" <o...@cs.otago.ac.nz>
Date: Thu, 9 Aug 2012 10:41:36 +1200
Local: Wed, Aug 8 2012 6:41 pm
Subject: Re: [erlang-questions] OtpErlangDouble/OtpErlangFloat

On 8/08/2012, at 11:01 PM, Martin Dimitrov wrote:

> Hello,

> I have some strings in Java representing floating point numbers. I want
> to transfer them to Erlang. So I create an object of type
> OtpErlangFloat. The problem is that is rounds the number funnily. For
> example,

> String s = "22.4";
> float f = Float.parseFloat(s);

Here you asked for a SINGLE-PRECISION floating point number.
I presume you did try

Prelude> 22.4 - 22.399999618530273
3.8146972514141453e-7

and you did notice that the difference is about the size of
single precision epsilon?
Well that's why.  You asked for a number with 24 bits of
precision that was close to 22.4, and 22.399999618530273
was indeed the answer you got.

> System.out.println(f); // 22.4

PrintStream.println(float) calls
PrintStream.print(float) calls
String.valueOf(float) calls
Float.toString(float)

whose documentation says (for this value of m) tha
it will be the integer part, then a dot, then "as
any, but only as many, more digits as are needed to
uniquely distinguish the argument value from adjacent
values of type 'float'".

So the value that is printed is the _least_ precise
decimal representation that cannot be mistaken for
a different single-precision (24 bits of precision)
float.

If you convert the same value to a double in Java and
print that in Java, you will get, well, try it:

L% cat NotErlangsFault.java
public class NotErlangsFault {
    public static void main(String[] args) {
        final String    s = "22.4";
        final float     f = Float.parseFloat(s);
        final double    d = (double)f;

        System.out.println(s + " converts to float " + f +
            " which as double is " + d);
    }

}

L% java NotErlangsFault
22.4 converts to float 22.4 which as double is 22.399999618530273

See?   Nothing to do with Erlang at all.

> OtpErlangFloat ef = new OtpErlangFloat(f);
> System.out.println(ef.toString()); // 22.399999618530273

And this tells us that an OtpErlangFloat contains a Java double,
which is *more* precise than the float you gave it.

> Then in Erlang:

> mochinum:digits(Ef).
> "22.399999618530273"

And this is the right answer.

> but

> mochinum:digits(22.4).
> "22.4"

> Is there a way to keep the precision when constructing
> OtpErlangDouble/OtpErlangFloat?

No precision whatsoever was lost in that step.

The precision was lost when you first converted a String
to a float.  The value in the float really *is* much
better approximated by 22.399999618530273 than by 22.4.

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin Dimitrov  
View profile  
 More options Aug 9 2012, 9:12 am
From: Martin Dimitrov <mrtndimit...@gmail.com>
Date: Thu, 09 Aug 2012 16:12:12 +0300
Local: Thurs, Aug 9 2012 9:12 am
Subject: Re: [erlang-questions] OtpErlangDouble/OtpErlangFloat
Thanks for the explanation. I know it is not Erlang to be blamed.

On 8/9/2012 1:41 AM, Richard O'Keefe wrote:

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »