Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In following code:

#define VMIN -210.0f
#define VMAX 220.0f
#define coef_a (0.5f*(VMIN+VMAX))
#define coef_b_green (510.0f/(VMAX-VMIN))
#define coef_b_red (510.0f/(VMIN-VMAX))

//#define LIMITUCHAR(a) ((a) < (255) ? (a) : 255)
uchar limit_uchar(float x)
{
	if (x<255.5f) 
	{
		if (x<0) return (uchar) 0;
		else return (uchar) x;
	}
	else return (uchar) 255;
}

/*
Generates a color as a function of v: if v<vmed, assign red, if v>vmed assign green
At the middle color=black
*/
void assigncolor(float v,uchar color[3])
{
	color[0]=0;
	color[1]=limit_uchar((v-coef_a)*coef_b_green);
	color[2]=limit_uchar((v-coef_a)*coef_b_red);
}



The following line generates compilation error due coef_b_green must be a modifiable lvalue
color[1]=limit_uchar((v-coef_a)*coef_b_green);


How can I avoid it?

What I have tried:

This is OK. coef_b_red does not create problem:

void assigncolor(float v,uchar color[3])
{
	color[0]=0;
	color[1]=limit_uchar((v-coef_a)*510.0f/(VMAX-VMIN));
	color[2]=limit_uchar((v-coef_a)*coef_b_red);
}


I also tried different braquet levels
Posted
Updated 13-Mar-18 3:44am

The following code
#include <iostream>
using namespace std;

typedef unsigned char uchar;

#define VMIN -210.0f
#define VMAX 220.0f
#define coef_a (0.5f*(VMIN+VMAX))
#define coef_b_green (510.0f/(VMAX-VMIN))
#define coef_b_red (510.0f/(VMIN-VMAX))

//#define LIMITUCHAR(a) ((a) < (255) ? (a) : 255)
uchar limit_uchar(float x)
{
  if (x<255.5f)
  {
    if (x<0) return (uchar) 0;
    else return (uchar) x;
  }
  else return (uchar) 255;
}

/*
Generates a color as a function of v: if v<vmed, assign red, if v>vmed assign green
At the middle color=black
*/
void assigncolor(float v,uchar color[3])
{
  color[0]=0;
  color[1]=limit_uchar((v-coef_a)*coef_b_green);
  color[2]=limit_uchar((v-coef_a)*coef_b_red);
}


int main()
{
  float v = 0.5;
  uchar c[3];
  assigncolor(v, c);

  for (const auto & x : c)
    cout << static_cast<unsigned int>(x) << endl;
}


Compiles and produces some output in my Linux box.
Please note, C++ offers better alternatives to #define, for instance, you might write
C++
constexpr float VMIN = -210.0f;
constexpr float VMAX = 220.0f;
constexpr float coef_a = (0.5f*(VMIN+VMAX));
constexpr float coef_b_green = (510.0f/(VMAX-VMIN));
constexpr float coef_b_red = (510.0f/(VMIN-VMAX));
 
Share this answer
 
CPallini is absolut correct, but I would go a big step further: #define are to avoid WHERE EVER possible because it complicates the readability of code AND the compiler (and so linker and optimization).

My compiler did compile with the above code. I guess that you have some typo (in your original code like invisible chars) or VS 2013 has some issues.

But really: avoid #define because that kills some code processing logic which will help you to write better code and so a better app

Think about the code flow in the range from 255. to 255.4999. Like:
C++
limit_uchar(255.3);

Depending how you "expect" your input the function may run a slight smoother
C++
uchar limit_uchar(float x)
{
  if (x>255.) {
     return (uchar) 255
  }
	
  if (x<0) {
    return (uchar) 0;
  }
  return (uchar) x;
}
By bringing the common cases at the top.
 
Share this answer
 
I solved the problem by changing:

#define VMIN -210.0f

By:
#define VMIN (-210.0f)


The problem was at the VS compiler line:

#define coef_b_green (510.0f/(VMAX-VMIN))

The compiler do not understand double --
#define coef_b_green (510.0f/(VMAX--210.0f))


NOTE: I accepted the solution-1 but using const:
const float VMIN =-210.0f;
const float VMAX =(220.0f);
const float coef_a= (0.5f*(VMIN+VMAX));
const float coef_b= (510.0f/(VMAX-VMIN));


As long as constexpr was not accepted by my VS2013 compiler.
As result it was increased the speed of assigncolor() function from 400 megaoperationss/second using #define to 454 MOPS using const float


unfortunalely if you arte using C or openCL you should use #define but setting negative values inside parentheses
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900