Why does the setter function in es6 return a celsius value instead of a farenheit value - es6-class

My question is about the setter function in the function makeClass().
set temperature(celsius) {
this.farenheit = celsius * 9.0 / 5 + 32;
}
Why does the code use the calculation F=C*9.0/5+32 which is the formula for farenheit and return 26 celsius instead of 26 farenheit as seen in the following part of the code that's outside the makeClass() function.
temp = thermos.temperature; // 26 in C
Below is the exercise from freecodecamp and it is correct but I don't actually understand how it works for the above reason.
function makeClass() {
"use strict";
/* Alter code below this line */
class Thermostat {
constructor(farenheit) {
this.farenheit = farenheit;
}
get temperature() {
return 5 / 9 * (this.farenheit - 32);
}
set temperature(celsius) {
this.farenheit = celsius * 9.0 / 5 + 32;
}
}
/* Alter code above this line */
return Thermostat;
}
const Thermostat = makeClass();
const thermos = new Thermostat(76); // setting in Fahrenheit scale
let temp = thermos.temperature; // 24.44 in C
console.log(temp)
thermos.temperature = 26;
temp = thermos.temperature; // 26 in C
console.log(temp)

The constructor expects you to pass it a value in fahrenheit and it stores that value as fahrenheit.
The getter is programmed to return a value in celsius so since the value was previously stored in fahrenheit, it has to convert the stored value to celsius.
The setter is programmed to accept a value in celsius, but store it in fahrenheit so it has to convert the value that was passed to fahrenheit.
I have no idea why the constructor was programmed to accept a value in fahrenheit, but the getter and setter deal in celsius. You'd have to ask the designer of this code why they did that. My guess is that it's just for demo purposes to show you how you could have a getter/setter that automatically handle unit conversions for you.

26 is passed into the parameter celsius in the setter function and converted into farenheit the result is passed into this.farenheit in the getter function and converted into celsius and returns 26 C when it's called outside the function.

Related

Decimal to hexadecimal conversion using CAPL

Is there any CAPL function for converting decimal value into hexadecimal value? I have already looked in help option of CAPL browser.
Assuming that you want the number to be converted to a string, that you can print out. Either to the write window, into the testreport, etc.
You can use snprintf like this:
snprintf(buffer,elcount(buffer),"%x",integervariable);
where buffer is a char array big enough.
This example is taken from the Vector knowledge base and was among the first result on google.
For hexadecimal equivalent value:
You can make use of _pow function (returns x to the power of y) and while in the following way, which would return you the hexadecimal equivalent value:
double decToHexEquivalent(int n)
{
int counter,remainder,decimal_number,hexadecimal_number = 0;
while(n!=0)
{
remainder = decimal_number % 16;
hexadecimal_number = hexadecimal_number + remainder * _pow(10, counter);
n=n/16;
++counter;
}
return hexadecimal_number;
}
you can call the above function in the following way:
testfunction xyz(int n)
{
write("Hexadecimal:%d", decToHexa(n));
}
Caution: not tested
For hexadecimal value
declare a global variable char buffer[100] in the variable section
variables
{
char buffer[100];
}
and then using snprintf function you can convert an integer variable to a character array, like this:
void dectohexValue(int decimal_number)
{
snprintf(buffer,elcount(buffer),"%02X",decimal_number);
}
then finally you can use the function as follows:
testfunction xyz(int n)
{
dectohexValue(n);
write("Hexadecimal:%s", buffer);
}

How do I pass a return value from one method to another?

I need to pass a return value from windCorrectionAngle method to groundSpeed method and place it at the very end of the expression ("windCorAng"). This is the only part that doesn't work for me. From what I learned it is not something that Java can do easily because it can't see the return from other methods. Would love to learn how to do that and what is a proper way of doing it. I have a simple print line to get the result of grSpd.
public double windCorrectionAngle()
{
double windCorAng = Math.toDegrees(Math.asin
( vw * Math.sin( Math.toRadians (w-d) ) / va) );
return windCorAng;
}
public double groundSpeed()
{
double grSpd = Math.sqrt( Math.pow(va,2) + Math.pow(vw,2) - 2 * va * vw *
Math.cos(Math.toRadians(d - w - windCorAng)));
return grSpd;
}
To use the return value of a function in an expression, just place a function call there, i. e.:
public double groundSpeed()
{
return Math.sqrt( Math.pow(va, 2) + Math.pow(vw, 2)
- 2 * va * vw * Math.cos(Math.toRadians(d - w - windCorrectionAngle()))
);
}

Where BigDecimal "/" is defined?

I thought '3.0'.to_d.div(2) is same as '3.0'.to_d / 2, but the former return 1 while latter returns 1.5.
I searched by def / in Bigdecimal's github repository, but I couldn't find it.
https://github.com/ruby/bigdecimal/search?utf8=%E2%9C%93&q=def+%2F&type=Code
Where can I find the definition? And which method is a equivalent to / in Bigdecimal?
In Float there is a fdiv method. Is there similar one in Bigdecimal?
You can find it in the source code of the bigdecimal library, in the repository you linked to. On line 3403 of ext/bigdecimal/bigdecimal.c, BigDecimal#/ is bound to the function BigDecimal_div:
rb_define_method(rb_cBigDecimal, "/", BigDecimal_div, 1);
This function looks like this:
static VALUE
BigDecimal_div(VALUE self, VALUE r)
/* For c = self/r: with round operation */
{
ENTER(5);
Real *c=NULL, *res=NULL, *div = NULL;
r = BigDecimal_divide(&c, &res, &div, self, r);
if (!NIL_P(r)) return r; /* coerced by other */
SAVE(c); SAVE(res); SAVE(div);
/* a/b = c + r/b */
/* c xxxxx
r 00000yyyyy ==> (y/b)*BASE >= HALF_BASE
*/
/* Round */
if (VpHasVal(div)) { /* frac[0] must be zero for NaN,INF,Zero */
VpInternalRound(c, 0, c->frac[c->Prec-1], (BDIGIT)(VpBaseVal() * (BDIGIT_DBL)res->frac[0] / div->frac[0]));
}
return ToValue(c);
}
This is because BigDecimal#div takes a second argument, precision, which defaults to 1.
irb(main):017:0> '3.0'.to_d.div(2, 2)
=> 0.15e1
However, when / is defined on BigDecimal,
rb_define_method(rb_cBigDecimal, "/", BigDecimal_div, 1);
They used 1 for the # of arguments, rather than -1 which means "variable number of arguments". So BigDecimal#div thinks it takes one required argument and one optional argument, whereas BigDecimal#/ takes one required argument and the optional arg is ignored. Because the optional argument is ignored, it's not initialized correctly, it gets an empty int or 0.
This may be considered a bug. You should consider opening an issue with the ruby devs.

Copying Vs Moving - Given code sample

I was going over some slides of Scott Meyers and it had the following code sample
typedef std::vector<T> TVec;
TVec createTVec(); // factory function
TVec vt;
…
vt = createTVec(); // in C++98, copy return value to - vt, then destroy return value
I dont understand the following points and would appreciate it if someone could clarify this:
Is createTVec an object on the stack of type TVec ? If so what are
() brackets next to it for ?
What does
vt = createTVec(); Was it suppose to be vt = createTVec; ??
It is a function, you have a declaration:
TVec createTVec(); // factory function
so it might look like:
TVec createTVec() {
TVec ret;
// initialize it
return ret;
}
What does vt = createTVec();
it assigns result from createTVec() function to vt variable.
In C++98 this function will return vector using temporary, since C++11 move semantics will be used. But, actually compiler might (and probably will) in such case do (N)RVO - Return Value Optimization.

how to rb_protect everything in ruby

I want to call ruby code from my own C code. In case an exception gets raised, I have to rb_protect the ruby code I call. rb_protect looks like this:
VALUE rb_protect(VALUE (* proc) (VALUE), VALUE data, int * state)
So proc has to be a function which takes VALUE arguments and returns VALUE. I have to call a lot of functions which do not work that way. How can I rb_protect them from raising exceptions?
I have thought of using Data_Make_Struct to wrap everything into one ruby object and call methods on it. Data_Make_Struct could itself raise an exception. How do I rb_protect Data_Make_Struct?
To use rb_protect in a flexible way (e.g., to call a Ruby function with an arbitrary numbers of arguments), pass a small dispatch function to rb_protect. Ruby requires that sizeof(VALUE) == sizeof(void*), and rb_protect blindly passes the VALUE-typed data to the dispatch function without inspecting it or modifying it. This means that you can pass whatever data you want to the dispatch function, let it unpack the data and call the appropriate Ruby method(s).
For example, to rb_protect a call to a Ruby method, you might use something like this:
#define MAX_ARGS 16
struct my_callback_stuff {
VALUE obj;
ID method_id;
int nargs;
VALUE args[MAX_ARGS];
};
VALUE my_callback_dispatch(VALUE rdata)
{
struct my_callback_stuff* data = (struct my_callback_stuff*) rdata;
return rb_funcall2(data->obj, data->method_id, data->nargs, data->args);
}
... in some other function ...
{
/* need to call Ruby */
struct my_callback_stuff stuff;
stuff.obj = the_object_to_call;
stuff.method_id = rb_intern("the_method_id");
stuff.nargs = 3;
stuff.args[0] = INT2FIX(1);
stuff.args[1] = INT2FIX(2);
stuff.args[2] = INT2FIX(3);
int state = 0;
VALUE ret = rb_protect(my_callback_dispatch, (VALUE)(&stuff), &state);
if (state) {
/* ... error processing happens here ... */
}
}
Also, keep in mind that rb_rescue or rb_ensure may be a better approach for some problems.

Resources