How to get Max Long value in dart - dart

Is there a way to get this in dart (java example):
Math.min(Long.MAX_VALUE, Long.MIN_VALUE);

In Dart, there is no direct representation of the Long data type. Both the Short and Long data types in Java are covered in the Dart int data type. See this question for more clarity.

Related

How to get the maxium value of an Integer in dart?

In java we can just Integer.maxValue but dart there is not this method.
I am using int max = 1<<32 but this doesn't work properly when compiling to javascript.
What is the best way to get the integer maximum value using dart language?
I was using dart_numerics package in my app for another reason and found it while typing
There is no method because the int maximum values are fixed.
According to the documentation:
Integers While the language specifies an arbitrarily sized integer,
for performance reasons the VM has three different internal integer
representations: smi (rhymes with pie), mint, and bigint. Each
representation is used to hold different ranges of integer numbers
(see the table). The VM automatically switches between these
representations behind the scenes as numbers grow and shrink in range.
You can read about that here: https://dart.dev/articles/archive/numeric-computation

Infinite length array in swift [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I noticed one thing in Haskell that it efficiently handle working with infinite length array with great ease.
So being swift programmer, I am curious about how we can achieve this with swift?
For example:
var infiniteArray = [1,2,3,.............]
Swift's Array stores concrete, eagerly evaluated elements, so it can't be infinite (due to finite memory).
The Swift equivalent is an infinite Sequence. Here's an example that produces the an infinite sequence of natural numbers.
let naturalNumbers = sequence(first: 0, next: { $0 + 1 })
let first5NaturalNumbers = Array(naturalNumbers.prefix(5))
print(first5NaturalNumbers)
It uses the sequence(first:next:) function to produce an UnfoldSequence, which is an infinitely long, lazy evaluated sequence.
Haskell has lazy evaluation because it is a fully functional language (has no side effects, etc.), Swift is not a functional language (though it borrows some features, functionality and syntax) and does not have the same features.
That being said you can use Sequences and Generators to create the impression of infinite lists - see http://blog.scottlogic.com/2014/06/26/swift-sequences.html for example. Of course the list is not really infinite - btw the Haskell list isn't infinite either, it just stores the function to create new entries.
The main difference is that in Haskell there are some major performance optimisations possible due to the lack of variables and side effects. In Swift you cannot do that. So be careful translating Haskell code to Swift.
If your intention to pass unknown number of parameters to a function (from a logical viewpoint, you can't say "infinite number of parameters" because of the limitation of the machine's memory), it's called variadic parameter:
A variadic parameter accepts zero or more values of a specified type.
You use a variadic parameter to specify that the parameter can be
passed a varying number of input values when the function is called.
Write variadic parameters by inserting three period characters (...)
after the parameter’s type name.
For example, let's say that you want to implement a function that takes unknown number of Ints to sum them:
func summationOfInfiniteInts(ints: Int...) -> Int {
return ints.reduce(0, +)
}
let summation = summationOfInfiniteInts(ints: 1, 2, 3, 4) // 10
Note that ints parameter in the block of summationOfInfiniteInts represented as [Int] (array of Int).
Hope this helped.

What is the max value of integer in dart?

I have looked everywhere but I couldnt find any information related to this topic.
Also, Is there a java - like Long / BigDecimal datatype in dart?
That depends if you are running in the Dart VM or compiling to JavaScript.
On the Dart VM an int is arbitrary precision and has no limit.
When compiling to JavaScript you are actually using floating point numbers, since that is all that JavaScript supports. This means you are restricted to representing integers within the range -253 to 253
Dart 2
For dart2js generated JavaScript Pixel Elephants answer is still true.
within the range -253 to 253
Other execution platforms have fixed-size integers with 64 bits.
The type BigInt was added to typed_data
Since Dart 2.0 will switch to fixed-size integers, we also added a BigInt class to the typed_data library. Developers targeting dart2js can use this class as well. The BigInt class is not implementing num, and is completely independent of the num hierarchy.
Dart 1
There are also the packages
- https://pub.dartlang.org/packages/bignum
- https://pub.dartlang.org/packages/decimal
I prefer this one:
int hugeInteger = double.maxFinite.toInt()

Convert CLLocationSpeed to long in Swift

I’m trying to get GPS speed in Swift. I've set everything up and am now trying to do some calculations. I want to convert the MS "string" to KMH. So I'm trying to convert the type to long, but I can’t get it to work.
I have tried this:
var ms: long?
ms = LocationManager.location.speed
But I got "CLLocationspeed is not convertible to long”
I’m new to iOS programming and Swift so I don't know how to fix this.
You should really start by reading the Swift Book, however:
long isn’t a standard type in Swift (not sure where you’ve managed to find one :). An appropriate integer type to use would be Int (unless exact size matters to you – but it probably doesn’t). But CLLocationSpeed in CoreLocation is a typealias for Double and you should probably stick with that for speed calculations.
In Swift, most conversions between types do not happen implicitly. If you really wanted a Double to become an Int, you need to explicitly convert it i.e. let ms = Int(LocationManager.location.speed).
This feels like a pain when you are coming from C-like languages. But there are good reasons behind it. For example, what should happen to the fractional part of the floating-point number when you assign it to an integer type? Might you have forgotten your function is returning a floating point number and have accidentally introduced a truncation bug?
To make up for this, Swift also has type inference. So unless you want to explicitly control the types, you don’t even have to give them:
// type of ms is automatically inferred to be CLLocationSpeed (alias for Double)
let ms = LocationManager.location.speed
// 3.6 floating-point literal is automatically converted to appropriate type
// type and kph is automatically a Double
let kph = ms * 3.6
Most of the time you don’t need to give a type, just leave it to be inferred it from the context.
Swift does not have a long datatype.
CLLocationSpeed is just a typealias for Double in Swift
So if you want to convert it into Double, do this
var ms = Double(LocationManager.location.speed)
The Swift compiler will automatically figure out the type for the variable
and then you can substitute Double(value) with Int, Float or any other Swift datatype
to typecast the value.

Idioms/Practices for Implementing Constrained Numeric Types in F#?

Suppose one needs a numeric data type whose allowed values fall within a specified range. More concretely, suppose one wants to define an integral type whose min value is 0 and maximum value is 5000. This type of scenario arises in many situations, such as when modeling a database data type, an XSD data type and so on.
What is the best way to model such a type in F#? In C#, one way to do this would be to define a struct that implemented the range checking overloaded operators, formatting and so on. A analogous approach in F# is described here: http://tomasp.net/blog/fsharp-custom-numeric.aspx/
I don't really need though a fully-fledged custom type; all I really want is an existing type with a constrained domain. For example, I would like to be able to write something like
type MyInt = Value of uint16 where Value <= 5000 (pseudocode)
Is there a shorthand way to do such a thing in F# or is the best approach to implement a custom numeric type as described in the aforementioned blog post?
You're referring to what are called refinement types in type theory, and as pointed out by Daniel, look for F*. But it is a research project.
As far as doing it with F#, in addition to Tomas' post, take a look at the designing with types series.
My suggestion would be to implement a custom struct wrapping your data type (e.g., int), just as you would in C#.
The idea behind creating this custom struct is that it allows you to "intercept" all uses of the underlying data value at run-time and check them for correctness. The alternative is to check all of these uses at compile-time, which is possible with something like F* (as others mentioned), although it's much more difficult and not something you would use for everyday code.

Resources