Page 2 of 3

Re: Type conversion not working? ESP32 Wroom

Posted: Sun Jul 24, 2022 1:50 pm
by mnfisher
I tested on an esp32 and it worked correctly...

Re: Type conversion not working? ESP32 Wroom

Posted: Sun Jul 24, 2022 1:54 pm
by mnfisher
I used the UART instead of a LCD - for ease...
Type conv.fcfx
(19.81 KiB) Downloaded 63 times

Re: Type conversion not working? ESP32 Wroom

Posted: Sun Jul 24, 2022 5:33 pm
by MJU20
This will be one of my greatest mystery's ever.

This is almost 1 on 1 the same as what I use in the other (larger) project.
I'm sure that the Float I'm trying to chop into bytes is a Float because I use it several times in the chart.
It always acts as a float, looks like a Float, tastes like a Float so I'm pretty sure it's a.. Float.

But in this other chart it looks like the conversion is not working.
So now I will try this somewhat further in this (example)chart and try to copy it into the larger project.

Again, thanks for your support.

Just for the sake of learning: how should I know that "24744/244" is stored as an INT where I ask to store it into a Float?
Is it I should have learned? Is this written somewhere? Is it that is convention in programming?

Re: Type conversion not working? ESP32 Wroom

Posted: Sun Jul 24, 2022 5:43 pm
by mnfisher
You need to add '.0' after one or both of the numbers - so 24744.0/244.0 would do the trick..

Re: Type conversion not working? ESP32 Wroom

Posted: Sun Jul 24, 2022 6:26 pm
by chipfryer27
Ah.... but did it smell like a Float..... you should sniff test anything that can vary :lol:

Re: Type conversion not working? ESP32 Wroom

Posted: Sun Jul 24, 2022 6:27 pm
by MJU20
I forgot to write that with the .0 it works!
I should have written that in my previous post. I'm sorry. :oops:

But then again, how is it that I should have known that a number is an INT?
(see: viewtopic.php?p=7593#p7593)

Re: Type conversion not working? ESP32 Wroom

Posted: Tue Jul 26, 2022 11:52 pm
by MJU20
This is awkward...

The versions that have passed here all worked after testing.
Now I use the same code in the original file and it refuses to work..

Don't get it.

The version I'm working on has a Float that doesn't contain a value when I start so I give it an initial value of 10.0
Then I write "dummy" bytes to the type conversion (set bytes) and convert them in the float.
This always returns a 0.00 for the Float. Why?

I need to remove all non needed code to post (again) an example.. will take a while..
:(

Re: Type conversion not working? ESP32 Wroom

Posted: Wed Jul 27, 2022 6:05 am
by chipfryer27
Hi

Only thing I can think on is to double check that the "dummy bytes" are actually bytes. These must be bytes and not integers etc.

Regards

Re: Type conversion not working? ESP32 Wroom

Posted: Wed Jul 27, 2022 6:42 am
by mnfisher
Type conversion should work either way.

If you can post a sample/pm your code I'll take a look..

Martin

Re: Type conversion not working? ESP32 Wroom

Posted: Wed Jul 27, 2022 7:06 am
by mnfisher
Here I stuff the values into a second TypeConversion - and display the result...

Seems to work okay..
Type conv.fcfx
(21.99 KiB) Downloaded 52 times
Note - one source of problems is that some values don't have an 'exact' representation as a float - so 3.000 might be 2.9999999 or 3.0000001 (probably not 3.0 - just an example of what happens) due to limitations in the float format (which is why for banking apps - values are stored as fixed point (2 integer) values)

So for comparisons - it is best to check a range (If x /y = z may not give the result you want - might need to do if (result > z - delta) && (result < z + delta))

Martin