I’m using doubles for some mathematical calculations and I’m getting these two values:

a = -69.49068165087722
n = 304.2372372266157

The problem is when I do this:

float fa = (float) a;
float fb = (float) b;

I get:
fa: -69.490685
fb: 304.23724

Losing a lot of the precision. Of course, this was expected but floats can’t reach at least 8 decimals? I can use six (like the fa variable in this example) but I also don’t know why the second value have even less decimal positions. How can I achieve floats of six decimals in both cases?

Thanks for your message. The thing is that I need to use floats (to send the values over OSC). How can I convert these doubles to floats but keeping at least 6 decimal positions?

You get:
resultd -75.36681365966797
resultffromd -75.36681

resultfromd have just five decimal positions. I would need six and I don’t know why in the conversion from double to float I’m losing one. Maybe it has to do that is a negative number?

Actually, sometimes I get six decimal values. When the integer part of the number have three positions then my decimal count goes down again (i.e.: 322.91475198111 goes 322.915 --losing three decimal positions from six possibles)

thanks, i see that now,
and it makes no difference if i use 75.36681540070782d;
so change the sign or the ‘d’ not help.
and float(double); even gives error

You might find some ideas in this previous discussion helpful:

If you need precision, you could still send your data over OSC – as double directly if it is supported, or with type conversion – depending on your speed / bandwidth needs, even sending your double as a string, for example.

I don’t think that Java floating point has fixed decimal precision – the precision depends on the values. Not sure that explains your issue entirely, but it sounds like floats are not the way to go in your case. Instead, maybe work around the OSC bottleneck…

double d = -75.36681540070782d;
float f = (float)(d);
int i = (int)(d);
float sendf = (float)( d - (double)(i) );
println(" d "+d+" f "+f+" i "+i);
println(" a hack could be to send 2 numbers: one integer "+i+" and a better float for the decimals only "+sendf);
double gotd = (double)(i) + (double)(sendf);
println(" on the other side combine it again: "+gotd);

see

d -75.36681540070782 f -75.36681 i -75
a hack could be to send 2 numbers:
one integer -75 and
a better float for the decimals only -0.3668154
on the other side combine it again: -75.36681538820267

Here’s an untested thought for you. Maybe try using the nf() function on your double, which creates a string, then convert it back to a number. I haven’t tested it but it may work using less code.

These things are all great workarounds in general cases. But if the goal is “I have doubles, and want to send them over OSC, so they have to become floats”… then no, they don’t have to become floats.

Just send OSC doubles. Problem solved.

import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
void setup() {
size(400,400);
frameRate(25);
oscP5 = new OscP5(this,12000);
myRemoteLocation = new NetAddress("127.0.0.1",12000);
}
void draw(){
background(0);
}
void mousePressed() {
OscMessage myMessage = new OscMessage("/test");
double d = 3.1415926535897932384626433832795028841971693993751;
// 50 digits -- more than will fit in a double
d += (int)random(100,900);
// it will store about n.1415927410126 or so
// w decimal matching up to n.141592
double a = -69.49068165087722;
double n = 304.2372372266157;
myMessage.add(d);
myMessage.add(a);
myMessage.add(n);
oscP5.send(myMessage, myRemoteLocation);
}
void oscEvent(OscMessage theOscMessage) {
// double math on the receiving end
print(millis(), "received", theOscMessage.typetag(), ": ");
double d = theOscMessage.get(0).doubleValue();
double a = theOscMessage.get(1).doubleValue();
double n = theOscMessage.get(2).doubleValue();
println(d, a, n);
// prints ~12 decimals of precision, xxx.141592741012xx
}

6818 received ddd : 147.14159274101257 -69.49068450927734 304.23724365234375
7930 received ddd : 494.1415927410126 -69.49068450927734 304.23724365234375
8851 received ddd : 345.1415927410126 -69.49068450927734 304.23724365234375