i want to map a continuous range of double values [10.0,20.0] to a byte in the range of [100,200];
The following must apply (even when converting back and forth multiple times):
convertToByte(convertToDouble(y)) == y
Thanks.
With help of the comments to the question we're using the following code:
int convertToByte(
double initial,
double minValue,
double maxValue,
int minByte,
int maxByte,
) {
double value = initial - minValue;
double valueRange = maxValue - minValue;
int byteRange = maxByte - minByte;
double valueSteps = valueRange / byteRange;
double byte = (value / valueSteps);
return (minByte + byte.round()).clamp(minByte, maxByte);
}
This does not provide a solution for the specified test, but a deterministic answer for a specific value.
When converting a byte back to a value and vice versa multiple times the output always stays the same.
This is what we needed for our application.
Related
Can't find or work out a solution to this that works with Dart.
I've already tried:
1. toStringAsFixed() - this rounds
2. double.parse(number.toStringAsFixed()) - this rounds
3. num - num % 0.01 - this rounds
I've found some solutions on SO but they either use functions that aren't available on Flutter/Dart or didn't work for me.
Any help would be greatly appreciated.
To build on #Irn beautiful Answer.
The function below lets you specify how many fractional digits / how many decimal places you want
double truncateToDecimalPlaces(num value, int fractionalDigits) => (value * pow(10,
fractionalDigits)).truncate() / pow(10, fractionalDigits);
Example
truncateToDecimalPlace(4321.92385678, 3) // 4321.923
Or my favorite way is to use Dart extensions
extension TruncateDoubles on double {
double truncateToDecimalPlaces(int fractionalDigits) => (this * pow(10,
fractionalDigits)).truncate() / pow(10, fractionalDigits);
}
This lets you call the function on a double object like thus
Usage
4321.92385678.truncateToDecimalPlaces(3); // 4321.923
Try
double truncateToHundreths(num value) => (value * 100).truncate() / 100;
There will be cases where you lose precision, but that requires you to use almost all 53 bits of double precision in the number (when multiplying by 100 loses precision to begin with).
Going trough strings is a significant overhead that I would avoid if possible, unless you actually expect to convert the number to a string anyway afterwards.
(value * 100).truncateToDouble() / 100
Example:
var lat = 29.4562
lat * 100 = 2945.62
2945.62 truncateToDouble() = 2945
2945 / 100 = 29.45
I have used the below method
String toFixed(double value, [int decimalPlace = 1]) {
try {
String originalNumber = value.toString();
List<String> formattedNumber = originalNumber.split('.');
return "${formattedNumber[0]}.${formattedNumber[1].substring(0, decimalPlace)}";
} catch (_) {}
return value.toString();
}
I want to assign two variables to integer and decimal parts on double.
how to do it?
One way would be
int x = abc.toInt()
int y = int.tryParse(abc.toString().split('.')[1]);
final double abc = 1.4;
int a = int.parse(abc.toString().split(".")[0]);
int b = int.parse(abc.toString().split(".")[1]);
Try this out, it should work fine
Three part question:
How to find 2 user created horizontal lines on a chart by name and return the price of each.
Then determine which HLine was crossed by the price most recently to determine trend direction.
Calculate Fibonacci levels based on prices and direction
double value = ObjectGetDouble(0,nameOfHLine,OBJPROP_PRICE1);
this is your value if you have name of the object, if you dont have it - need to loop over all objects:
string name;
for(int i=ObjectsTotal()-1;i>=0;i--){
name = ObjectName(i);
if(ObjectType(name)!=OBJ_HLINE) continue;
}
Working example of Fibonacci object that can be edited by the user and printing of fibonacci levels.
#include <ChartObjects/ChartObjectsFibo.mqh>
CChartObjectFibo *Fibo;
int OnInit()
{
Fibo = new CChartObjectFibo();
#Create object and set some defaults
if(!Fibo.Create(0,"Fibonacci",0,Time[5],Open[5],Time[0],Open[0]))
{
return(INIT_FAILED);
}
# Allow user to drag object
Fibo.Selectable(true);
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
delete Fibo;
}
void OnTick()
{
string level_description;
double level_value;
string printString="Fibonacci Levels - ";
# Get the two anchor prices
double p1 = Fibo.GetDouble(OBJPROP_PRICE,0);
double p2 = Fibo.GetDouble(OBJPROP_PRICE,1);
# Calculate range
double range=MathAbs(p1-p2);
for(int i=0;i<Fibo.LevelsCount();i++)
{
level_description=Fibo.LevelDescription(i);
# Calculate price of level
level_value=(p2>p1)?p2-range*Fibo.LevelValue(i):p2+range*Fibo.LevelValue(i);
printString=StringFormat("%s %s:%.5f",printString,level_description,level_value);
}
Print(printString);
}
Difficult to understand exactly what you are after, not sure if you are trying to find the graphical objects or just calculate levels based on the prices. Assuming you have the price of the two horizontal lines, the following structure and function can be used to calculate Fibonacci levels. (price 1 is earlier in time than price 2).
Calculation based on formula found here
struct FibLevel {
double retrace38;
double retrace50;
double retrace61;
double extension61;
double extension100;
double extension138;
double extension161;
};
void FibLevel(double price1, double price2,FibLevel &fiblevel)
{
double range = MathAbs(price1-price2);
fiblevel.retrace38 =(price1<price2)?price2-range*0.382:price1+range*0.382;
fiblevel.retrace50 =(price1<price2)?price2-range*0.500:price1+range*0.500;
fiblevel.retrace61 =(price1<price2)?price2-range*0.618:price1+range*0.618;
fiblevel.extension61 =(price1<price2)?price2+range*0.618:price1-range*0.618;
fiblevel.extension100=(price1<price2)?price2+range :price1-range;
fiblevel.extension138=(price1<price2)?price2+range*1.382:price1-range*1.382;
fiblevel.extension161=(price1<price2)?price2+range*1.618:price1-range*1.618;
}
For my math question app I have two random numbers generated then I have 4 buttons as answers. I want to check the answer if the user pushes the right button but it seems not to work.
num1 and num2 are the labels which the random numbers are generated in so technically
num1.text = "(randomnum1)"
and num2.text = "(randomnum2)" Thanks.
I have the following code under button1 IBaction
var sum = (num1) + (num2)
if btn1.titleLabel = (sum){
check.text = "right"
}
Maybe you should do some convert before you add num1 and num2. (convert strings to integers before adding them up, also, you should convert string to integer fisrt when comparing sum and btn.titleLabel)
you have two options:
if btn1.titleLabel.toInt()! == sum
or
if btn1.titleLabel == String(sum)
consider the difference between = (assign) and == (is equal)
to assign a number to a label use
num1.text = "\(randomnum1)"
I recommend to use backing Int variables like randomnum1 to hold the random numbers which can used for the math for example
var randomnum1 = 0, randomnum2 = 0 // instance variables of the class
randomnum1 = randomFuntion()
randomnum2 = randomFuntion()
num1.text = String(randomnum1)
num2.text = String(randomnum2)
now the labels contain the string values of the random numbers, but you can do the math with the associated variables.
var sum = randomnum1 + randomnum2
after that you can check the result as mentioned above
if btn1.titleLabel == String(sum) {
check.text = "right"
}
I've looked at the answers for converting int's to floats and other similar answers but they don't do exactly what I want.
I'm trying to create a basic program that takes a number does some different calculations onto it and the results of those calculations are added together at the end.
For one of those calculations I created a segmented controller with the 3 different values below
var myValues: [Double] = [0.00, 1.00, 1.50]
var myValue = [myValuesSegmentedController.selectedSegmentIndex]
then when one of those values is picked, it's added to the final value. All the values added together are Doubles to 2 decimal places.
var totalAmount = valueA + valueB + valueC + myValue
the problem I'm having is that swift won't let me add "myValue" to those final calculations. It gives me the error:
Swift Compiler Error. Cannot invoke '+' with an argument list of type '($T7, #lvalue [int])'
What do I need to do to change that value to a Double? Or what can I do to get a similar result?
You can cast it with Double() like this
var totalAmount = valueA + valueB + valueC + Double(myValue)
The problem is you are trying to add an array instead of an Int, so You don't even need to convert anything, considering that all of your values are already Doubles and your index actually has to be an Int. So
let myValues = [0.00, 1.00, 1.50]
let myValue = [myValuesSegmentedController.selectedSegmentIndex] // your mistake is here, you are creating one array of integers with only one element(your index)
The correct would be something like these:
let myValues = [0.00, 1.00, 1.50]
let totalAmount = myValues.reduce(0, combine: +) + myValues[myValuesSegmentedController.selectedSegmentIndex]
Put this in a playground:
var myValues: [Double] = [0.00, 1.00, 1.50]
let valueA = 1
let valueB = 2
let valueC = 3
var totalAmount = Double(valueA + valueB + valueC) + myValues[2]
println(totalAmount) //output is 7.5
valueA/B/C are all inferred to be Int.
totalAmount is inferred to be a Double
To convert a float to an integer in Swift. Basic casting like this does not work because these vars are not primitives, unlike floats and ints in Objective-C:
var float:Float = 2.2
var integer:Int = float as Float