Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Testing a value in a range

Posted on 2003-10-26
7
Medium Priority
?
247 Views
Last Modified: 2012-05-04
Hi there gurus.

I am writing a program that converts numerical values into colors

For example

if value is between 0.0 and 0.01 color value is RGB(255,255,0)
                            0.011 and 0.02 color value is RGB(255,255,1)
                                        ...
                                        ...
                                        ...
                            0.091 and 0.1  color value is RGB(255,255,255)
There will be up to 1000 of these ranges
Any idea how to code this.

Regards

Peter
0
Comment
Question by:Pishky
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 32

Expert Comment

by:jhance
ID: 9622214
Many options here:

1) if...else if...else if...else...
2) Table lookup.  If this, write a quick/dirty program to generate your data tables rather then hand-code them.
3) Algorithm.  The above ranges, I assume, are for example only.  But your real situation most likely follows a pattern that can be modeled with an algorithm and implemented in code.
0
 
LVL 6

Expert Comment

by:GaryFx
ID: 9622220
If there are only a few ranges, you can just chain if-else statements.  If there are a lot, then store the RGB values in an array, and convert the ranges to integers (e.g. irange = static_cast<int>(range*100).  This second approach pays the cost of a float->int conversion, but it's much cleaner and easier to maintain, assuming your ranges are all hundredths apart.

Gary
0
 

Author Comment

by:Pishky
ID: 9622253
Thank you for the above but I am not sure that this is answering my question. The suggestions above imply a single mapping from one value to another. The problem I have is to map a range of values to a single value.

Regards

Peter
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Accepted Solution

by:
GaryFx earned 375 total points
ID: 9622265
No, they don't.  Note the multiplication by 100 in my cast:

If r is in the range [0.01, 0.02) (using the standard meaning of [ for inclusive and ) for exclusive), then r*100 will be between 1.0 and 2.0, so that static_cast<int>(r) will be 1.  That's how you get the mapping of an entire range to a single int.

Also, your ranges were (0.01, 0.02] (i.e. exclusive at the low end and inclusive at the high end).  This could be an issue, but more likely it won't matter because most of these decimals can't be represented exactly in binary anyway.

Gary
0
 
LVL 3

Expert Comment

by:monkesdb
ID: 9623131
Well

Why not simply use the integer version of the colour.

All the colours are represented by a number in the range [0, 0xFFFFFF]. If your number is in the range [0, 1) just multiply by 2^24. Then if you need to convert it to rgb it's simple.

Value = (long) (YourNumber * 0x1000000);
Red = (Value 0xFF0000) >> 16;
Green = (Value 0xFF00) >> 8;
Blue = (Value 0xFF);

If you want to convert RGB to your number then...

Value = (Red << 16) + (Green << 8) + Blue;
YourNumber = (float) Value / 0x1000000;

0
 
LVL 9

Expert Comment

by:tinchos
ID: 10546155
No comment has been added lately, so it's time to clean up this question.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: GaryFx {http:#9622265}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question