user85427: I am using this example on how to use a photoresistor to detect the value of light that the sensor is getting but I want to convert the reading of the resistor to a percentage but I can't use the map() function from the Arduino docs but I can implement something similar to it
so I created this simple function
float photo_resistor(int pinNum)
{
    float reading = analogRead(pinNum);
    if (reading == 54)
    {
     return 0.0;
    }
    return (reading - 54)/(100/920);

}

as in the docs I want to use an output range from 0 to 100 and the minimum reading i got is 54 and the maximum is 974
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
// this becomes (x - 54)*(100 - 0 ) / ( 974 - 54) + 0;
// (x-54)/(100/920);

but this does not output a value from on the scale from 0-100


user7508: You wrote:

return (reading - 54)/(100/920);


In C++, 100/920 is zero. This is because, when both operands are
integers, the division operator performs an integer division. You can
overcome this problem by making sure that at least one of the operands
is a floating point number. For instance, 100.0/920 is roughly 0.1087.
However, your division is backwards. You probably mean
return (reading - 54) * (100.0/920);


85427: <p>I am using <a href="http://developer.wildernesslabs.co/Hardware/Tutorials/Electronics/Part5/Resistive_Sensor_Lab/" rel="nofollow noreferrer">this example</a> on how to use a photoresistor to detect the value of light that the sensor is getting but I want to convert the reading of the resistor to a percentage but I can't use the <code>map()</code> function from the Arduino <a href="https://www.arduino.cc/reference/en/language/functions/math/map/" rel="nofollow noreferrer">docs</a> but I can implement something similar to it</p>
<p>so I created this simple function</p>
<pre><code>float photo_resistor(int pinNum)
{
    float reading = analogRead(pinNum);
    if (reading == 54)
    {
     return 0.0;
    }
    return (reading - 54)/(100/920);

}
</code></pre>
<p>as in the docs I want to use an output range from 0 to 100 and the minimum reading i got is 54 and the maximum is 974</p>
<pre><code>return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
// this becomes (x - 54)*(100 - 0 ) / ( 974 - 54) + 0;
// (x-54)/(100/920);
</code></pre>
<p>but this does not output a value from on the scale from 0-100</p>
<p><a href="https://i.stack.imgur.com/DnoNl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DnoNl.png" alt="enter image description here" /></a></p>
 7508: <p>You wrote:</p>
<blockquote>
<pre class="lang-cpp prettyprint-override"><code>return (reading - 54)/(100/920);
</code></pre>
</blockquote>
<p>In C++, <code>100/920</code> is zero. This is because, when both operands are
integers, the division operator performs an integer division. You can
overcome this problem by making sure that at least one of the operands
is a floating point number. For instance, <code>100.0/920</code> is roughly 0.1087.</p>
<p>However, your division is backwards. You probably mean</p>
<pre class="lang-cpp prettyprint-override"><code>return (reading - 54) * (100.0/920);
</code></pre>
