floats not giving expected results

//ADC_prescaler = 5000/1024; //Compiler bug gives 4.00 not 4.88 as expected

Hi folks, I'd like to look "under the hood" as it were, to see what the pre-processor is doing with floats. It feels like they're get cast to ints, which is not really useful when I'm talking to sensors like the LM35 temp sensor. I know that other compilers are documented with respect to the pre-processor, could someone please point me to where I can find this documentation for the Arduino IDE ?

tia, ged

Hi folks,

Greetings.

//ADC_prescaler = 5000/1024; //Compiler bug gives 4.00 not 4.88 as expected

This is not a compiler bug.

I'd like to look "under the hood" as it were, to see what the pre-processor is doing with floats.

The preprocessor does nothing with floats.

It feels like they're get cast to ints,

In the example you posted, the numbers are not getting cast to int. The numbers ARE ints.

I know that other compilers are documented with respect to the pre-processor, could someone please point me to where I can find this documentation for the Arduino IDE ?

Any decent C++ documentation will do.

If you want the compiler to use floating-point arithmetic, you only need to ask...

ADC_prescaler = 5000[glow].0[/glow]/1024[glow].0[/glow];

Hi again,

Well then, why is there a sneaky line in the preferences file : preproc.substitute_floats=true

I've been programmer/systems analyst/administrator for a while now and every compiler, even the bash shell gives you a float when you you divide two large ints. unless the result is an int. Please don't assume that because I'm new to this forum, that I'm new to everything :'(

Please don't assume that because I'm new to this forum, that I'm new to everything

Welcome, we call this planet "Earth"

Sorry, couldn't resist... ;)

Hi again,

Hello.

Well then, why is there a sneaky line in the preferences file : preproc.substitute_floats=true

Ask the Processing folks. It's not used by Arduino. (but I'll check later tonight just in case)

I've been programmer/systems analyst/administrator for a while now and every compiler, even the bash shell gives you a float when you you divide two large ints. unless the result is an int.

While that may be true for every programming language you've used it is not true for every programming language. It has never been true for C(++).

Please don't assume that because I'm new to this forum, that I'm new to everything

I've made no assumptions about you. Please do not confuse terse for condescending.

From the Processing documentation...

compiler.substitute_floats (currently "substitute_f") treat doubles as floats, i.e. 12.3 becomes 12.3f so that people don't have to add f after their numbers all the time since it's confusing for beginners.

The "preproc.substitute_floats" option appears nowhere in the Arduino source code. It is not used by Arduino.

Hi Coding Badly,

Apologies for the late reply. I take your point, I didn’t realise there is an ‘us and them’ thing happening in the Arduino community. But, I digress, the code I shared in my first post is misleading, I should have put the whole line in there I’ve added a.0 as per your suggestion
float ADC_prescaler = (5000.0/1024); //(5000/1024)// for a 5v Arduino

I still feel that the compiler should cope and give the required result

regards,
gt7

I still feel that the compiler should cope and give the required result

It did give the required result. The required result was not the desired result, though.

To get the desired result, you have to work [u]with[/u] the compiler.

I didn’t realise there is an ‘us and them’ thing happening in the Arduino community

Not sure what you mean there.

I've been programmer/systems analyst/administrator for a while now

Not nearly long enough, if you make a claim like this:

every compiler, even the bash shell gives you a float when you you divide two large ints.

Wrong in multiple ways:

$ echo $(( 222222221 / 111111111 ))
1
$

Bash doesn't magically convert to float, and it is not a compiler. C/C++ doesn't convert, it does what you tell it. Fortunately FORTRAN, Pascal, Ada, etc are far enough in the past I can't remember for sure (and I'm too lazy to look 'em up), but I don't think they magically convert, either. I think Perl does.

-j

I pipe stuff through 'bc' in BASH to get floating point....