I would like to enter values like z = 5e-3 (that is, z = 0.005) and w = 5e6 into my code.
I've looked through documentation on the pow10 function, but of course something like double pow10(int -9); isn't allowed.
I'm using Code::Blocks 13.12 for C++ on Windows 7.
Any ideas?
double z = 5e-3;
works, or alternatively
double z = 5*pow(10, -3);
(I presume you're using C, or C++. You didn't specify in the question or the tags; but the "cmath" tag was used.)
Related
In FreeBasic, IIf (A, B, C) is equivalent to A ? B : C in C/C++/Java/C#, If (A, B, C) in VB.NET, and B if A else C in python. However, in VB6, Office VBA and VB.NET, IIf (A, B, C) is equivalent to this C++ code:
bool a = A;
auto b = B;
auto c = C;
a ? b : c;
As a result, IIf works differently in Visual Basic and FreeBasic. For example,
Dim x As Integer, y As Integer
x = 0
y = IIf (x <> 0, 12 \ x, 0)
works normally in FreeBasic, but will throw an exception in Visual Basic. That is because in Visual Basic this code is equivalent to the following C++ code:
int x, y;
x = 0;
bool a = (x != 0);
auto b = 12 / x;
auto c = 0;
y = a ? b : c;
In VB.NET, using If will do the same thing as using IIf in FreeBasic. For example,
Dim x As Integer, y As Integer
x = 0
y = If (x <> 0, 12 \ x, 0)
in VB.NET will not throw exception.
The question is: Why does IIf (A, B, C) in VB6/VBA evaluate both B and C, not selecting one expression to evaluate depending on the value of expression A?
IIf is a function. It works like any other function. It doesn't know anything about the expressions used to generate its arguments; only their values. Any expressions you write in your code to pass to IIf are evaluated first, just as they would be for any other method call.
VB.NET added an If operator in 2008. Because it's an operator, it's behaviour is baked into the language and short-circuiting is part of that, just as it is part of the behaviour of the ternary operator (?:) in C-based languages.
Only the FreeBASIC developers can answer this in the affirmative, but here is some plausible speculation:
VB.NET strives to be backwards-compatible to VBA/VB6. The (non-short-circuiting) IIf function already existed in VBA/VB6. Thus, making it short-circuiting would break backwards-compatibility (consider IIf(x, SomeMethodAWithSideEffects(), SomeMethodBWithSideEffects()). Thus, they opted to create a new operator If(...) instead.¹
FreeBASIC, on the other hand, never claimed/tried to be compatible to VBA/VB6, just to QuickBASIC: Consider, for example, their object-oriented features, which have completely different syntax. QuickBASIC never had a IIf function, so they could use the keyword with slightly different semantics without having to worry about backwards-compatibility.
Now, the obvious follow-up question is: Why doesn't VBA/VB6's IIf operator short-circuit? Again, I can only speculate, bit I assume that they made IIf non short-circuiting for consistency. It would have been surprising for a Basic dialect to have something that looks like a regular function (e.g. IIf(a, b, c)) suddenly to have short-circuiting behavior. But, again, that's just an educated guess. Unless one of the original developers accidentally sees this question, I'm not sure we're going to get an authoritative answer.
¹ In the first Beta version of the .NET Framework, they changed And and Or to be logical short-circuiting operators and introduced new BitAnd/BitOr operators for the original VBA/VB6 behavior. They had to revert that change in the RTM version and introduce the new AndAlso/OrElse operators instead, to avoid breaking backwards-compatibility.
My F# program runs well but sometimes I find it difficult to understand the flow. I define values that are not functions in many modules. When these modules are opened these values are bound. I would like to obtain a list of all the names of non-function values and in which modules they were declared. Is this possible?
The example below may clarify the question.
module A =
let i = 1
let add x y = x + y
module B =
let x = 99.9
let sqr z = z * z
open A
open B
let y =
add (float i) x
|> sqr
printfn "%f" y
Is there a function foo that would do the following?
let nonFuncVals = foo()
printfn "%A" nonFuncVals
// ["A.i", "B.x"]
There is no built-in support for this. Depending on how you work, you might be able to do this in some hacky way, but it is probably going to have quite a few limitations.
The following works on your example, when you run it using F# interactive, but I'm sure there are many ways in which it can break:
open System.Reflection
let nonFuncVals () =
let special = set [ "it"; "CheckClose"; "LastGenerated" ]
[ for t in Assembly.GetExecutingAssembly().GetTypes() do
if t.FullName.StartsWith "FSI_" then
for p in t.GetProperties() do
if not (special.Contains p.Name) then
if t.FullName.Length <= 8 then yield p.Name
else yield t.FullName.Substring(9) + "." + p.Name ]
|> Seq.distinct
nonFuncVals()
The function looks at the currently defined types and uses the fact that F# Interactive puts generated bindings in types with names such as FSI_0001. This is, however, undocumented behaviour and it can change in the next version...
I want a bunch of buttons, using QtGui to all have their own unique values, but when looping to create a grid of them, the button variable is overwritten.
I was trying to get something that would have each button have its own variable, like grid_btn01, grid_btn02, and so on.
Ideally, it would be like this
for x in range(gridx):
grid_btn + str(x) = GridBtn(self, x, y, btn_id)
But of course, this doesn't work.
consider using a python dictionary
,also i'm not familiar with Qt but double check what is the return value of this function maybe btn_id is the variable you should store
buttons = {}
for x in range(gridx):
buttons[x] = GridBtn(self,x,y,btn_id)
What you're asking may technicallybe possible in Python, but it's definitely the wrong approach.
Use a list instead:
grid_btns = []
for x in range(gridx):
y = ...
grid_btns.append(GridBtn(self, x, y, btn_id))
I am trying to solve the following equation in my program:
7.7^2 x 0.012^2/(0.2145 x 1.67^(16/3))
That should equal : 0.002582 (this is verified w/ google & scientific calculator)
This is the code that I am using
CGFloat eX1 = pow(7.7, 2) * pow(0.012, 2)/(0.2145 * pow(1.67, (16/3)));
NSLog(#"%f",eX1);
And even though, I believe my code should give me the same results, it's actually giving me:0.002679
What am I doing wrong? What can I do to obtain the correct answer?
Change (16/3) to (16.0/3.0). Otherwise 16/3 results in 5, not 5.33333349.
And you have 7.2 instead of 7.7 at the start.
I am using F# Interactive, and I have added the reference of FSharp.PowerPack.dll.
When I try to convert BigNum to double as the following code,
let n = 2N
let d = double n
error comes out that
"System.MissingMethodException: Method not found: 'Double System.Numerics.BigInteger.ToDouble(System.Numerics.BigInteger)'. at Microsoft.FSharp.Math.BigNum.ToDouble(BigNum n)"
What can I do if I want to make such conversions like "BigNum to int" and "BigNum to double"? Thanks very much.
What you've written will work in the F# standalone CTP.
This error occurs in VS2010, because the BigInteger type has been moved from the F# library to the .Net4.0 core library. I'm not sure whether this issue has something to do with having both the F# CTP and the VS2010 beta installed.
Until a better solution comes along, you could roll your own conversion like this:
let numToDouble (n:bignum) = double n.Numerator / double n.Denominator
To convert a bignum to an integer you could then think of something like this:
let numToInt (n:bignum) = int n.Numerator / int n.Denominator
But this is rather dangerous: it'll overflow quite easily. A better version of numToInt would be to convert to a double first and then convert to int:
let numToInt = int << numToDouble
Still, both conversions aren't ideal for numerators/denominators of more than 308 digits, which will still overflow a double, while the fraction itself could be small.
ex: 11^300 / 13^280 ~= 3.26337, but
> numToDouble (pown 11N 300 / pown 13N 280);;
val it : float = nan