Complex Math in arduino

…and please remember that is memory used by the program that it knows about at ‘compile time’, you need to allow more for your use of runtime variables, stack & heap requirements as the program executes.

if you know about the Jean Meeus algorithm maybe it can provide understanding, so I have a plan to make a program to display the astronomical data of the Sun and the Moon using the Jean Meeus algorithm, because I am still new to programming so I am asking this, like the question at the beginning whether in the Arduino program can count numbers with precision such as displaying multiple numbers after a comma.

is there any chance that a program packaged with a library will take up less memory than a program that is written directly without the library? or the library only makes simple programs without affecting memory or in other words packing programs with libraries or not packaging them has no effect on memory. I hope you understand what I mean, sorry I'm still a layman so I have a lot of questions.

Hello
Change your headline to a description your are searching for.
Otherwise all people will think in arrays and matrixes.
Have a nice day and enjoy coding in C++.

i don't know what i should do

Among the things you should not do is post poor quality pictures without any explanation.

You should post your sketch using code tags; the easiest is

  1. In the IDE, use tools -> auto format
  2. In the IDE, use edit-> copy for forum
  3. Paste in a reply

You should post the errors

  1. Use the copy error messages button in the IDE to copy the errors to the clipboard
  2. Click the code tag button (</>) above the reply window. You should see something like
    ```
    type or paste code here
    ```
  3. Overwrite the type or paste code here with the copied error message.

You should tell us for which board you compile.

Don't worry about how much precision your floats can manage - write the code and see if it's accurate enough for your needs.

I suspect that you may have been misled by the fact that floats are printed with only two decimal points by default. That has nothing to do with how precise they actually are and you can change how many are displayed like this:

Serial.println(MyFloat,4); // Show four decimal places.

Stop worrying about memory till such time that you run out of it. With an Uno and an OLED you will probably quite quickly run out of memory, with a Mega less so.

See the first reply :smiley:

1 Like

You have a major problem with the equation. The compiler defaults to doing arithmetic with int data type (16-bit signed integer).

In your equation, (bln-1)/12 will produce 0 when bln<=12, or 1 when bln=13, because the result is stored as an int before the addition. The same goes for tgl/365, since tlg (presumably the day of the month) will always be less than 365, the result will always be 0.

  float Tahun = thn + (bln-1)/12 + tgl/365;

You can force the compiler to do the math in floats by specifying that at least one of the numbers in each equation is a float:

int tgl = 13;
int bln = 6;
int thn = 2020;

void setup(){
  Serial.begin(115200);
  Serial.println();
  float Tahun = thn + (bln-1)/12 + tgl/365;
  Serial.print("Tahun with int math: ");
  Serial.println(Tahun);
  Tahun = thn + (bln-1)/12.0 + tgl/365.0;
  Serial.print("Tahun with float math: ");
  Serial.print(Tahun, 6);
}

void loop(){
  
}

sorry I'm a new user so I don't understand this rule

[code]

#include <BigNumber.h>
#include <math.h>
// Posisi Matahari dan Bulan Low Accuracy Jean Meeus

  int l=-6,b=109,tgl=13,bln=6,thn=2020,j=1,mnt=0,dtk=0,zw=(b/15);
  int M,Y,A,B;

  float Tahun = thn+(bln-1.0)/12.0+tgl/365.0;

 
  

void setup()
{
  
  Serial.begin (115200);
  Serial.println ();
  Serial.println ();
  BigNumber::begin ();

  BigNumber JDU;
  BigNumber JDE;
  BigNumber T;
  BigNumber JDUT;
  BigNumber TuJD;
  BigNumber GST_UT;
  BigNumber GST_L;
  BigNumber LST;
  BigNumber delta;
  
  if (bln<3) 
  M = bln+1;
else 
  M = bln;

if (bln<3) 
  Y = thn-1;
else 
  Y = thn;

A=(Y/100);

B = (2+int (A/4)-A);

if (Tahun>2005)
  if(Tahun<=2050)
    delta = 62.92 + 0.32217*(Tahun-2000) + 0.005589*(Tahun-2000)*(Tahun-2000);
  else 
    delta = 0; 
else
  delta = 0;

JDU = 1720994.5+int32_t(365.25*Y)+int32_t(30.60001*(M+1))+B+tgl+(j+mnt/60+dtk/3600)/24 - zw/24;
JDE = JDU+(delta/86400);
T = (JDE-2451545)/36525;
JDUT = 1720994.5+int32_t(365.25*Y)+int32_t(30.60001*(M+1))+B+j;
TuJD = (JDUT-2451545)/36525;  
GST_UT = fmod(6.6973745583+2400.0513369072*TuJD+0.0000258622*TuJD*TuJD,24);
GST_L = fmod(GST_UT+(j+mnt/60+dtk/3600)*1.00273790935,24);
LST = fmod(GST_L+b/15,24);

Serial.println(LST);
[/code][quote="sagise, post:31, topic:946934, full:true"]
sorry I'm a new user so I don't understand this rule
[/quote]

I attached the sketch that I made, but there was an error that I couldn't understand

I tried to fix the sketch, but I get a page full of errors: https://wokwi.com/arduino/projects/320875391557304914 [EDIT] I fixed the errors.
Can you give a link to where you found that code ?
I don't like the mix of integers and float and at least many round brackets should be added to show what the calculation is doing. Now that the BigNumber is added, it has become a lot worse.

Did you take existing code and then added BigNumber ? That is not possible, the BigNumber library is not a drop-in-replacement for floating point calculations.

Since this path is going downhill, can you get a Arduino board with double precision floating point and use that ?

1 Like

There are several permanent reference threads at the top of this sub forum, designed specifically for new users. You will find the full explanation there.

1 Like

after I understood earlier, it turns out that what you said is true, bignumbers cannot be used for complex calculations,
maybe the only way is to use an arduino DUE which has a double with bytes big

1. If you want 6/7 digit accuracy after the decimal point, it is single precision (Fig-1). It can be achieved by using Arduino UNO or NANO or MEGA which supports float-type data.
binary32x
Figure-1:

2. If you want 15/16 digit accuracy after the decimal point, it is double precision (Fig-2). It can be achieved by using Arduino DUE Board which supports double-type data.

binary64
Figure-2:

1 Like

in wokwi.com is it also available for arduino DUE simulation?

That is a VERY misleading way to describe the number of significant digits in floating point numbers, unless you make it clear that the numbers must first be normalized to have a mantissa between 0.0 and 1.0! Most people unfamiliar with floating point formats do NOT understand this absolutely critical distinction.

2 Likes

Can you explain your reasoning behind that assertion, please?

(Again, I need to ask - do you really mean complex, or simply "non-integer"?)

1 Like

Looks like part of your calculation is for the Julian date, not sure of some of the other calculations. Do you just need the date, or do you intend to do more calculations with it? It is quite easy to get the Julian date from the unix timestamp, which is what the Time library uses, and generating a few decimal places accuracy with only integer math can be done.

2 Likes